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吗?到底错在哪呢?#include <stdio.h> int same(int n1,int n2) { int i; if(n1>=n2) { for(i=n2;i>0;i--) if(n1%i==0&&n2%i==0) return i; } else { for(i=n1;i>0;i--) if(n1%i==0&&n2%i==0) return i; } } int main() { char o; char str[20]; int a,b,c,d,n1,n2,s,k; while(scanf("%s",str)!=EOF) { a=str[0]-48; b=str[2]-48; c=str[4]-48; d=str[6]-48; o=str[3]; n2=b*d; if(o=='+') n1=a*d+b*c; if(o=='-') n1=a*d-b*c; if(n1==0) printf("0\n"); if(n1%n2==0) printf("%d\n",n1/n2); else { if(n1<0) k=-n1; else k=n1; s=same(k,n2); n2=n2/s; n1=n1/s; printf("%d/%d\n",n1,n2); } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator