标签:
/* 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; }
标签:
原文地址:http://www.cnblogs.com/flipped/p/5922369.html