专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Perl/Python

请帮小弟我看一下怎么用python重新编写这个c程序

发布时间:2011-06-29 19:58:53 文章来源:www.iduyao.cn 采编人员:星星草
请帮我看一下如何用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:
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: