[問題] 分數相加化簡,一直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