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 |
很坑,給幾組樣例吧,這幾組過了應該就木問題了注意幾個問題: 1,分呣是1的時猴不输出 2,分呣小于洞的時猴分呣分子取反 3,注意区分数中的负號和式子的减號 4,好像没有超int范围的test case input: 231/2+235/2 output: 233 input: 1024/2-512/1 output: 0 input: -233/2333-233/-2333 output: 0 input: 1/-5--1/7 output: -2/35 附代妈: #include <stdio.h> #include <string.h> #include <cmath> using namespace std; int gcd(int a,int b){ a=abs(a+0.0),b=abs(b+0.0); if(!a)return b; if(!b)return a; if(a<b)return gcd(a,b%a); return gcd(b,a%b); } int main() { char exp[233]; while(scanf("%s",exp)>0){ int l = strlen(exp); char op; int st = 0; int s1; int a=0,b=0,c=0,d=0; bool fa=0,fb=0,fc=0,fd=0; for(int i = 0; i < l; i++){ switch(st){ case 0: if(exp[i]=='/') { st = 1; s1=i; } else if(exp[i]=='-') fa=1; else{ a*=10; a+=exp[i]-'0'; } break; case 1: if(exp[i]=='+'||(i>s1+1 && exp[i]=='-')){ op = exp[i]; st = 2; } else if(exp[i]=='-') fb=1; else{ b*=10; b+=exp[i]-'0'; } break; case 2: if(exp[i]=='/') st = 3; else if(exp[i]=='-') fc=1; else{ c*=10; c+=exp[i]-'0'; } break; case 3: if(exp[i]=='-') fd=1; else{ d*=10; d+=exp[i]-'0'; } break; default: break; } } if(fa)a=-a; if(fb)b=-b; if(fc)c=-c; if(fd)d=-d; int fm=b*d,fz=a*d+b*c*((op=='+')?1:-1),g=gcd(fm,fz); fm/=g,fz/=g; if(fm>>31){ fm=-fm; fz=-fz; } if(!fz)printf("0\n"); else if(!(fm-1))printf("%d\n",fz); else printf("%d/%d\n",fz,fm); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator