USACO Section 2.1 Ordered Fractions

模拟,开始时以为要注意去重,当输出时,判断与前一个是否重复,后来发现没这必要,如果重复了,那后边那组肯定不是互质的

analysis里给出了“super fast solution"的方法,类似杨辉三角,想起了高中时的阿炳老师--两个肩上数之和~~

 

 1 /* ID:linyvxi1
2 PROB:frac1
3 LANG:C++
4 */
5 #include <stdio.h>
6 #include <algorithm>
7 using namespace std;
8 typedef struct Frac{
9 int up;
10 int down;
11 }Frac;
12 Frac frac[100000];
13 int total_solutions=1;
14 bool cmp(Frac a,Frac b)
15 {
16 return ((float)a.up/a.down)<((float)b.up/b.down);
17 }
18 int gcd(int a,int b)
19 {
20 int temp;
21 while(b){
22 temp=a%b;
23 a=b;
24 b=temp;
25 }
26 return a;
27 }
28 void trans(int n)
29 {
30 int i;
31 for(i=1;i<n;i++){
32 if(gcd(n,i)==1){
33 frac[total_solutions].up=i;
34 frac[total_solutions++].down=n;
35 }
36 }
37 }
38
39 int main()
40 {
41 int n;
42 freopen("frac1.in","r",stdin);
43 freopen("frac1.out","w",stdout);
44 scanf("%d",&n);
45 int i=1;
46 for(i=2;i<=n;i++){
47 trans(i);
48 }
49 sort(frac+1,frac+total_solutions,cmp);
50 printf("0/1\n");
51 for(i=1;i<total_solutions;i++){
52 printf("%d",frac[i].up);
53 putchar('/');
54 printf("%d\n",frac[i].down);
55 }
56 printf("1/1\n");
57 }



转载于:https://www.cnblogs.com/yangce/archive/2012/02/16/2354562.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值