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 |
试了无数次了,自己运行各种情况都考虑了啊,怎么还错啊!帮忙看看吧,,,(附代码)#include "stdio.h" #include "math.h" int GCD(int a,int b) { if(!b) return a; else return GCD(b,a%b); } int main() { int gcd,sum; char a[10]; while(scanf("%s",a)!=EOF) { a[0]=(int)a[0]-48; a[2]=(int)a[2]-48; a[4]=(int)a[4]-48; a[6]=(int)a[6]-48; if(a[0]>=0&&a[0]<=9&&a[2]>=0&&a[2]<=9&&a[4]>=0 &&a[4]<=9&&a[6]>=0&&a[6]<=9) { gcd=GCD(a[2],a[6]); a[0]=a[0]*a[6]/gcd; a[4]=a[4]*a[2]/gcd; a[6]=a[2]=a[2]*a[6]/gcd; if(a[3]=='+') { sum=a[0]+a[4]; gcd=GCD(sum,a[6]); sum/=gcd;a[6]/=gcd; if(sum==a[6]) {printf("1\n");continue;} printf("%d/%d\n",sum,a[6]); } if(a[3]=='-') { sum=a[0]-a[4]; gcd=GCD(sum,a[6]); sum/=gcd;a[6]/=gcd; if(sum*a[6]<0) { sum=abs(sum); a[6]=abs(a[6]); if(sum==a[6]) {printf("-1\n");continue;} printf("-%d/%d\n",sum,a[6]); } else { if(sum==a[6]) {printf("1\n");continue;} printf("%d/%d\n",sum,a[6]); } } } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator