看了好几篇博客,终于AC了
参考:1081. Rational Sum (20)-PAT甲级真题 – 柳婼 の blog
浙大 PAT 甲级 1081 Rational Sum 分数运算 排错清单 错误原因_马铃薯小弟的博客-CSDN博客pat-1081 Rational Sum(20)(分数运算之注意细节系列)_欢迎访问方偲的博客-CSDN博客
总结一下个人遇到的错误:
1、一开始没将输入数据设置为long long型
2、没有特殊处理整数部分为0,分子部分也为0的情况
3、测试点3错误,在每一次读取完分子分母就进行约分,修改后结果正确(防止最后求和时分子溢出)
AC代码:
#include#include #include using namespace std; int gcd(long long x, long long y) { if (x < y) swap(x, y); if (x % y == 0) return y; else return gcd(y, x % y); } vector numer; vector denom; int main() { int N; cin >> N; numer.resize(N); denom.resize(N); long long g = 1; for (int i = 0; i



