请帮我看一下如何用python重新编写这个c程序
这个是c语言程序:
#include <limits.h>
void f_and_s(int [], int, int, int *, int *);
void first_second(int x[], int n, int *first, int *second)
{
f_and_s(x, 0, n-1, first, second);
}
void f_and_s(int x[], int left, int right, int *f, int *s)
{
int mid;
int F1, F2; /* returned smallest items */
int S1, S2; /* returned second smallest */
if (left > right) /* range empty ? */
*f = *s = INT_MAX; /* YES, return INT_MAX */
else if (left == right) /* exactly one item ? */
*f = x[left], *s = INT_MAX; /* return it and inf*/
else {
mid = (left + right)/2; /* now cut from middle */
f_and_s(x, left, mid, &F1, &S1); /* left */
f_and_s(x, mid+1, right, &F2, &S2); /* right */
if (F1 < F2) /* pick 1st and 2nd items. */
*f = F1, *s = (S1 < F2) ? S1 : F2;
else
*f = F2, *s = (S2 < F1) ? S2 : F1;
}
}
/* ------------------------------------------------------ */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAXSIZE 100
int main(void)
{
int x[MAXSIZE];
int n, first, second;
int i;
char line[100];
printf("\nRecursive First-Second Elements");
printf("\n===============================");
printf("\n\nHow many elements (at least 2) --> ");
gets(line);
n = atoi(line);
srand((unsigned) clock());
printf("\nRandomly Generated Data :\n");
for (i = 0; i < n; i++) {
x[i] = rand();
if (i % 10 == 0) printf("\n");
printf("%6d", x[i]);
}
first_second(x, n, &first, &second);
printf("\n\nThe Smallest Element --------->%6d", first);
printf( "\nThe Second Smallest Element -->%6d", second);
getchar();
return 0;
}
///
我用python重写了一下
#____________________________________#
import random
first=[]
second=[]
INT_MAX=2147483647
MAXSIZE=100
x=[]
#____________________________________#
def f_and_s(x,left,right,mjfirst,mjsecond):
F1=F2=S1=S2=[]
F1+=[""]
F2+=[""]
S1+=[""]
S2+=[""]
if(left>right):
mjfirst[0]=INT_MAX
mjsecond[0]=INT_MAX
elif(left==right):
print "ssss",mjfirst[0]
mjfirst[0]=x[left]
mjsecond[0]=INT_MAX
print "mjken ",mjfirst[0],id(mjfirst[0])
elif(left<right):
mid=(left+right)/2
print mid
f_and_s(x,left,mid,F1,S1)
f_and_s(x,mid+1,right,F2,S2)
if(F1[0]<F2[0]):
mjfirst[0]=F1[0]
if(S1[0]<F2[0]):
mjsecond[0]=S1[0]
else:
mjsecond[0]=F2[0]
else:
mjfirst[0]=F2[0]
if(S2[0]<F1[0]):
mjsecond[0]=S2[0]
else:
mjsecond[0]=F1[0]
def first_second(x,n,firstl,secondl):
print "sdfsd",firstl[0]
f_and_s(x,0,n-1,firstl,secondl)
for i in range(0,MAXSIZE):
x+=[""]
print "Recursive First-Second Elements"
print "==============================="
n=raw_input("\nHow may elements (at least 2)-->")
n=int(n)
print "Randomly Generated Data:"
for i in range(0,n):
x[i]=random.randrange(1,6000)
if(i%10==0):
print ""
print x[i],
first+=[""]
second+=[""]
first_second(x,n,first,second)
print "\nThe Smallest Element---------->",first[0]
print "The Second Smallest Element-->",second[0]
///
但执行起来就是有问题
因为我不懂得如何在递归里面层层递归
希望知道的朋友麻烦说一下
谢谢
这个程序是想在一组数组中快速求出最小的数和第二小的数
Recursive First-Second Elements
===============================
How many elements (at least 2) --> 41
Randomly Generated Data :
15906 2034 20815 9854 25100 20801 25717 29969 24203 8799
8073 12747 29363 18975 12766 15176 32005 32271 18334 26268
12691 18440 4363 31114 31589 29187 20235 5367 3354 25646
2276 9117 25810 10560 20292 8452 18252 3337 12420 19630
30284
The Smallest Element ---------> 2034
The Second Smallest Element --> 2276
[解决办法]
可以测试下你的算法和Python内建的min(),max()方法的性能和占用资源谁更有优势,呵呵
[解决办法]
也太乱了吧。