http://train.usaco.org/usacoprob2?a=ZSn5SwI7CNy&S=frac1
没想到这么简单。。直接1A
需要注意的问题:
①两个int相除要得到double,int前必须加(double)进行强制类型转换
②结构体排序,compare函数
代码如下:
/*
ID: 49743541
PROG: frac1
LANG: C++
*/
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef struct{
int zi;
int mu;
double value;
}fenshu;
bool compare(fenshu A,fenshu B){
if(A.value<B.value)
return true;
return false;
}
fenshu f[32400];
int n;
bool isrp(int a, int b){
if(a==1||b==1)
return true;
while(1){
int t = a%b;
if(t == 0) break;
else{
a = b;
b = t;
}
}
if(b>1) return false;
else return true;
}
int main(){
freopen("frac1.in","r",stdin);
freopen("frac1.out","w",stdout);
scanf("%d",&n);
int p = 0;
for(int i = 1;i<=n;i++)
for(int j = i;j<=n;j++){
if(isrp(i,j)){
f[p].mu = j;
f[p].zi = i;
f[p].value = (double)i/(double)j;
p++;
}
}
sort(f,f+p,compare);
printf("0/1\n");
for(int i = 0;i<p;i++)
printf("%d/%d\n",f[i].zi,f[i].mu);
return 0;
}