首页 > 其他 > 详细

【USACO 2.1】Ordered Fractions

时间:2016-09-30 02:15:40      阅读:84      评论:0      收藏:0      [点我收藏+]
/*
TASK: frac1
LANG: C++
URL: http://train.usaco.org/usacoprob2?S=frac1&a=dbgwn5v2WLr
SOLVE: 直接枚举,约分,排序,去重
 */
#include<cstdio>
#include<algorithm>
using namespace std;
struct node{
	int nu,deno;
	double v;
}a[40000];
int n,cnt;
int cmp(node a,node b){
	return a.v<b.v;
}
int gcd(int a,int b){
	return b?gcd(b,a%b):a;
}
int main(){
	freopen("frac1.in","r",stdin);
	freopen("frac1.out","w",stdout);
	scanf("%d",&n);
	for(int i=0;i<=n;i++)
		for(int j=max(i,1);j<=n;j++){
			int g=gcd(i,j);
			a[cnt++]=(node){i/g,j/g,i*1.0/j};
		}
	sort(a,a+cnt,cmp);
	for(int i=0;i<cnt;i++){
		if(i==0||a[i].v-a[i-1].v>1e-6||a[i].v-a[i-1].v<-1e-6)
		printf("%d/%d\n",a[i].nu,a[i].deno);
	}
	return 0;
}

  

【USACO 2.1】Ordered Fractions

原文:http://www.cnblogs.com/flipped/p/5922369.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!