当前位置: 代码迷 >> 综合 >> PAT 甲级 1088 PAT Ranking 个人错误总结
  详细解决方案

PAT 甲级 1088 PAT Ranking 个人错误总结

热度:32   发布时间:2024-01-28 04:55:25.0

写的很乱,因为每个函数只用了一次,懒得写成函数了。
注意点,-4/3输出为-1 1/3 而不是-1 -1/3。。。
注意longlong ,如果long请全long,否则后果不堪设想。。

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct f{long long up,down;
}a,b;
long long gcb(long long a,long long b){if(b==0)return a;else return gcb(b,a%b);
}
f clean(f a){if(a.down<0){a.up=-a.up;a.down=-a.down;}if(a.up==0){a.down=1;}else{long long y=gcb(abs(a.up),abs(a.down));a.up/=y;a.down/=y;}return a;
}
void prin(f a){a=clean(a);if(a.down==0){printf("Inf");}else if(a.down==1&&a.up<0){printf("(%lld)",a.up);}else if(a.down==1&&a.up>=0){printf("%lld",a.up);}else if(abs(a.up)>=abs(a.down)&&a.up<0){printf("(%lld %lld/%lld)",a.up/a.down,-a.up%a.down,a.down);}else if(abs(a.up)>=abs(a.down)){printf("%lld %lld/%lld",a.up/a.down,a.up%a.down,a.down);}else if(a.up<0){printf("(%lld/%lld)",a.up,a.down);}else{printf("%lld/%lld",a.up,a.down);}}
int main(){scanf("%lld/%lld %lld/%lld",&a.up,&a.down,&b.up,&b.down);a=clean(a);b=clean(b);
// b.up/=gcb(b.up,b.down);
// b.down/=gcb(b.up,b.down);f jia,jian,cheng,chu;jia.up=a.up*b.down+b.up*a.down;jia.down=a.down*b.down;jia=clean(jia);jian.up=a.up*b.down-b.up*a.down;jian.down=a.down*b.down;jian=clean(jian);cheng.up=a.up*b.up;cheng.down=a.down*b.down;cheng=clean(cheng);chu.up=a.up*b.down;chu.down=a.down*b.up;chu=clean(chu);prin(a);printf(" + ");prin(b);printf(" = ");prin(jia);printf("\n");prin(a);printf(" - ");prin(b);printf(" = ");prin(jian);printf("\n");prin(a);printf(" * ");prin(b);printf(" = ");prin(cheng);printf("\n");prin(a);printf(" / ");prin(b);printf(" = ");prin(chu);printf("\n");return 0;
}