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 |
果断被阴了,需要考虑分子为0分母为1和分子等于分母。没有测试数据。小弟总结了几个。希望对后来者有一点点帮助 0/9+5/1 5 0/9-5/1 -5 5/1+4/1 9 9/2-1/2 4 0/1+0/5 0 想到的就这么多。呵呵。注意好最开始输入为分母1和约分后分母为1 #include<stdio.h> #include<math.h> int LowestNumber(int a,int b); int main(){ char ch; int a1,b1,a2,b2; while(scanf("%d/%d%c%d/%d",&a1,&b1,&ch,&a2,&b2)==5&&(a1!=EOF||b1!=EOF||a2!=EOF||b2!=EOF)){ a1*=b2;a2*=b1;b1*=b2;b2=b1; if(ch=='+'){ a1+=a2; if(!a1||b1==1){ printf("%d\n",a1); }else{ a2=LowestNumber(a1,b1); a1/=a2;b1/=a2; if(a1==b1||b1==1) printf("%d\n",a1/b1); else printf("%d/%d\n",a1,b1); } }else{ a1-=a2; if(!a1||b1==1){ printf("%d\n",a1); }else{ a2=LowestNumber(a1,b1); a1/=a2;b1/=a2; if(a1==b1||b1==1) printf("%d\n",a1/b1); else printf("%d/%d\n",a1,b1); } } } return 0; } int LowestNumber(int a,int b){ int i,j,n; n=abs(a)>abs(b)?abs(a):abs(b); if(n==1){ return 1; }else{ for(i=1,j=0;i<=n;i++) if(a%i==0&&b%i==0) j=i; return j; } } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator