标签:des style ar io color os sp strong on
将a/b和c/d化简一下就可以了
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12588 | Accepted: 4206 |
Description
Input
Output
Sample Input
1/8+3/8 1/4-1/2 1/3-1/3
Sample Output
1/2 -1/4 0
Source
#include<stdio.h> int gcd(int a,int b) { return b==0?a:gcd(b,a%b); } int main() { int a,b,c,d,sum1,sum2,f,flag; char o; while(~scanf("%d/%d%c%d/%d",&a,&b,&o,&c,&d)) { flag=0; sum2=b*d; if(o=='+') { sum1=a*d+b*c; } else if(o=='-') { sum1=a*d-b*c; } f=gcd(sum1,sum2); if(sum1==0) printf("0\n"); else if(sum2/f==1) printf("%d\n",sum1/f); else { if(sum1<0&&sum2<0) { sum1=-sum1; sum2=-sum2; } else if(sum1<0&&sum2>0) { sum1=-sum1; flag=1; } else if(sum1>0&&sum2<0) { sum2=-sum2; flag=1; } f=gcd(sum1,sum2); if(flag==0) printf("%d/%d\n",sum1/f,sum2/f); else if(flag==1) { printf("-%d/%d\n",sum1/f,sum2/f); } } } return 0; }
标签:des style ar io color os sp strong on
原文地址:http://blog.csdn.net/qq_16767427/article/details/41955943