标签:style io 数据 div ar 时间 amp table
1/8+3/8 1/4-1/2 1/3-1/3
刚开始提交有错误,(1/1=1,8/2=4/1) 将其改为(1/1=1,8/2=4) 才AC掉
#include<stdio.h> int gcd(int a,int b) { if(a%b!=0) gcd(b,a%b); else return(b); } int main() { int a,b,c,d,x,y,z,t; char o; while(scanf("%d/%d%c%d/%d",&a,&b,&o,&c,&d)!=EOF) { if(o=='+') { x=gcd(b,d); y=d/x*a+b/x*c; z=b*d/x; if(y==0) printf("0\n"); else if(y==z) printf("1\n"); else { t=gcd(y,z); if(z/t==1) printf("%d\n",y/t); else printf("%d/%d\n",y/t,z/t); } } else if(o=='-') { x=gcd(b,d); y=d/x*a-b/x*c; z=b*d/x; if(y==0) printf("0\n"); else if(y<0) { y=-y; if(y==z) printf("-1\n"); else { t=gcd(y,z); if(z/t==1) printf("-%d\n",y/t); else printf("-%d/%d\n",y/t,z/t); } } else { if(y==z) printf("1\n"); else { t=gcd(y,z); if(z/t==1) printf("%d\n",y/t); else printf("%d/%d\n",y/t,z/t); } } } } return 0; }
标签:style io 数据 div ar 时间 amp table
原文地址:http://blog.csdn.net/phytn/article/details/38273119