Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
所有的数据都对了,但是一直是run time error!!#include "stdio.h" //辗转相除法求最大公约数 int gcd(int m, int n) { if (n == 0) { return m; } if (m < n) { int tmp = m; m = n; n = tmp; } gcd(n,m%n); } void fun(int a, int b, int c, int d, int o) { a = a*d; c = c*b; //准备计算出结果的分子n跟分母m int n; o ? n = a+c : n = a-c; int m = b*d; //求出公约数 int mnGcd = gcd(m, n > 0 ? n : -n); //分子分母约分 n = n/mnGcd; m = m/mnGcd; //判断是否是整数 if(n%m == 0) { printf("%d\n",n/m); } else { printf("%d/%d\n",n,m); } } int main() { char str[8]; while(scanf("%s",&str)!=EOF) { fun(str[0]-'0',str[2]-'0',str[4]-'0',str[6]-'0',str[3]=='+'); } } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator