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> int gcd(int a,int b) { return b==0?a:gcd(b,a%b); } int main() { int a,b,c,d,m,n,s; char op; while(scanf("%d/%d%c%d/%d",&a,&b,&op,&c,&d)!=EOF) { m=b*d/gcd(b,d); a=a*(m/b); c=c*(m/d); switch(op) { case'+': n=a+c; s=gcd(m,n); if(s==m) printf("%d\n",n/s); else printf("%d/%d\n",n/s,m/s); break; case'-': if(a<c) { printf("-"); n=c-a; s=gcd(m,n); if(m==s) printf("%d\n",n/s); else printf("%d/%d\n",n/s,m/s); break; } else if(a==c) { printf("0\n"); break; } else { n=a-c; s=gcd(m,n); if(m==s) printf("%d\n",n/s); else printf("%d/%d\n",n/s,m/s); break; } } } return 1; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator