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 |
还有什么情况没考虑么?WHY WA??附代码#include<iostream.h> #include<string.h> int main() { char a[10]; char b[10]; while(cin>>a>>b) { if(a[0]=='0'&&b[0]=='0')return 0; int n1=strlen(a); int n2=strlen(b); int n; if(n1>n2)n=n2; else n=n1;//求出较小者的位数 int k=0,h=0; for(int i=0;i<n;i++) { int x=a[n1-1-i]-48; int y=b[n2-1-i]-48; if(x+y+h>=10) { k++; h=1; } else h=0; }//分别从末尾向前走,计进的位数 int j=0;//计 如1+9999 时的可能进位 if(n1>n2&&h==1) { for(i=n1-1-n;i>=0;i--) { if(a[i]=='9')j++; else break; } } else if(n1<n2&&h==1) { for(i=n2-1-n;i>=0;i++) { if(b[i]=='9')j++; else break; } } k+=j;//计算最终进位 if(k==0)cout<<"No carry operation."<<endl; else { if(k==1)cout<<"1 carry operation."<<endl; else cout<<k<<" carry operations."<<endl;//对0,1和其他这三种情况分别处理 } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator