HIT OJ 1076 哈工大OJ

 

Ordered Fractions

 Source : Unknown
 Time limit : 3 sec Memory limit : 32 M

Submitted : 1140, Accepted : 349

Consider the set of all reduced fractions between 0 and 1 inclusive with denominators less than or equal to N.

Here is the set when N = 5:

0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1

Write a program that, given an integer N between 1 and 160 inclusive, prints the fractions in order of increasing magnitude.

INPUT FORMAT

Several lines with a single integer N in each line. Process to the end of file.

SAMPLE INPUT

5
5

OUTPUT FORMAT

One fraction per line, sorted in order of magnitude. print a blank line after each testcase.

SAMPLE OUTPUT

0/1
1/5
1/4
1/3
2/5
1/2
3/5
2/3
3/4
4/5
1/1

0/1
1/5
1/4
1/3
2/5
1/2
3/5
2/3
3/4
4/5
1/1


直接枚举分子分母,然后排序输出即可。水题。
 
我的代码:
#include<stdio.h>
#include<algorithm>

using namespace std;

struct node
{
	int a;
	int b;
	double cur;
};

int gcd(int a,int b)
{
	if(b==0)
		return a;
	else
		return gcd(b,a%b);
}

bool cmp(node a,node b)
{
	return a.cur<b.cur;
}

int main()
{
	int n,i,j,num=0;
	node acm[10000];
	while(scanf("%d",&n)!=EOF)
	{
		num=0;
		for(i=1;i<=n;i++)
		{
			for(j=0;j<=i;j++)
			{
				if(gcd(i,j)==1)
				{
					acm[num].a=j;
					acm[num].b=i;
					acm[num].cur=(j*1.0)/(i*1.0);
					num++;
				}
			}
		}
		sort(acm,acm+num,cmp);
		for(i=0;i<num;i++)
			printf("%d/%d\n",acm[i].a,acm[i].b);
		printf("\n");
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值