#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int gcd(int n,int c){
if(n%c==0){
return c;
}
else{
return gcd(c,n%c);
}
}
int gbs(int x,int y){
if(x>=y){
for(int i=1;;i++){
if(x*i%y==0){
return x*i;
}
}
}
for(int i=1;;i++){
if(y*i%x==0){
return i*y;
}
}
}
struct fs{
int fm,fz;
}a[100010];
bool cmp(fs a,fs b){
return a.fz<b.fz;
}
int main(){
freopen("C.in","r",stdin);
freopen("C.out","w",stdout);
int n,ans=1;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d/%d",&a[i].fz,&a[i].fm);
ans=gbs(a[i].fm,ans);
}
for(int i=1;i<=n;i++){
a[i].fz=(ans/a[i].fm)*a[i].fz;
a[i].fm=ans;
}
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++){
printf("%d/%d\n",a[i].fz/gcd(a[i].fz,a[i].fm),a[i].fm/gcd(a[i].fz,a[i].fm));
}
return 0;
}
共 6 条回复
#include #include #include using namespace std; struct fs { long long int fm, fz; char fh; double c;
} a[100005]; bool cmp(fs a, fs b) { return a.fz * b.fm < a.fm * b.fz; } int w(int a, int b) { if (a % b == 0) { return b; } else { return w(b, a % b); } } int main() { freopen("C.in", "r", stdin); freopen("C.out", "w", stdout); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i].fz >> a[i].fh >> a[i].fm; } sort(a + 1, a + 1 + n, cmp); for (int j = 1; j <= n; j++) { cout << a[j].fz / w(a[j].fm, a[j].fz) << "/" << a[j].fm / w(a[j].fm, a[j].fz) << endl; } return 0; }
万分感谢
我是这么做的
超了七个测试点
怎么错了