USACO 2.1 Ordered Fractions(乱搞)

水题,尼玛,数组开大不给MLE,来了个看不懂的错误,不就多申请点内存了嘛。。。

 1 /*

 2  ID: cuizhe

 3  LANG: C++

 4  TASK: frac1

 5 */

 6 #include <cstdio>

 7 #include <cstring>

 8 #include <cmath>

 9 #include <algorithm>

10 using namespace std;

11 struct node

12 {

13     int a,b;

14     double x;

15 } p[160*160+1];

16 int cmp(const node &a,const node &b)

17 {

18     if(a.x < b.x)

19         return 1;

20     else

21         return 0;

22 }

23 int gcd(int a,int b)

24 {

25     return b == 0?a:gcd(b,a%b);

26 }

27 int main()

28 {

29     int i,j,n,num;

30     freopen("frac1.in","r",stdin);

31     freopen("frac1.out","w",stdout);

32     scanf("%d",&n);

33     p[0].a = 0;

34     p[0].b = 1;

35     p[0].x = 0;

36     p[1].a = 1;

37     p[1].b = 1;

38     p[1].x = 1;

39     num = 2;

40     for(i = 2; i <= n; i ++)

41     {

42         for(j = 1; j <= i-1; j ++)

43         {

44             if(gcd(i,j)==1)

45             {

46                 p[num].a = j;

47                 p[num].b = i;

48                 p[num].x = j*1.0/i;

49                 num ++;

50             }

51         }

52     }

53     sort(p,p+num,cmp);

54     for(i = 0; i <= num-1; i ++)

55         printf("%d/%d\n",p[i].a,p[i].b);

56     return 0;

57 }

 

你可能感兴趣的:(action)