[问题] 分数相加化简,一直wa

楼主: criticalbird (好啊)   2015-05-14 16:35:49
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
GCC
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)

问题(Question):
是在nthu OJ的10478
网址如下:http://acm.cs.nthu.edu.tw/contest/709/
[Description]
Given several fractions, compute their sum and express the answer in the
simplest fraction form.
[Input]
There are many test cases in one subtask.
The first line of each test case contains an integer t, which indicates the
number of fractions in the input. Each of the following t lines contains two
integers a and b, which represent the numerator and the denominator of a
fractio
喂入的资料(Input):
3
7 3
5 2
1 6
预期的正确结果(Expected Output):
5/1
错误结果(Wrong Output):
我也是得到5/1,但怎么跑就是Wrong Answer orz
程式码(Code):(请善用置底文网页, 记得排版)
#include <stdlib.h>
#include <stdio.h>
int hi, lo;
int getGCD(int N1, int N2){
int n1 = N1, n2 = N2;
int mod=0;
if(n1>n2){
int temp = n1;
n1 = n2;
n2 = temp;
}
while(1){
mod = n2%n1;
if(mod<0)
mod += n1;
if(mod==0){
return n1;
}
else{
n2 = n1;
n1 = mod;
}
}
return 0;
}
void addF(int a, int b){
int newHi, newLo;
newLo = lo*b;
newHi = hi*b + a*lo;
int gcd = getGCD(newHi, newLo);
hi = newHi/gcd;
lo = newLo/gcd;
}
int main(){
int i;
int t, a, b;
scanf("%d", &t);
for(i=0; i<t; i++){
if(i==0)
scanf("%d %d", &hi, &lo);
else{
scanf("%d %d", &a, &b);
addF(a, b);
}
}
printf("%d/%d\n", hi, lo);
return 0;
}
补充说明(Supplement):
作者: TobyH4cker (Toby (我要当好人))   2015-05-14 18:04:00
是不是没换行呢?Sample Output是5/1换行才EOF唷sorry我没看程式码XD还是说你没有用循环来接受input

Links booklink

Contact Us: admin [ a t ] ucptt.com