作者criticalbird (好啊)
看板C_and_CPP
標題[問題] 分數相加化簡,一直wa
時間Thu May 14 16:35:49 2015
開發平台(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):
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 42.72.62.221
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1431592552.A.536.html
※ 編輯: criticalbird (42.72.62.221), 05/14/2015 16:39:56
→ TobyH4cker: 是不是沒換行呢?05/14 18:04
→ TobyH4cker: Sample Output是5/1換行才EOF唷05/14 18:05
→ TobyH4cker: sorry我沒看程式碼XD05/14 18:05
→ TobyH4cker: 還是說你沒有用迴圈來接受input05/14 18:08
是的XD 後來經提醒後發現是沒用迴圈的關係! 謝謝大大!
※ 編輯: criticalbird (42.72.62.221), 05/14/2015 19:01:50