#include<iostream>
#include<string>
using namespace std;
int stringAdd(string s1, string s2){
int lenBig, lenSmall;
if(s1.length()>s2.length()){
lenBig=s1.length();
lenSmall=s2.length();
}else{
lenBig=s2.length();
lenSmall=s1.length();
}
int carryTime=0;
int carryNum=0;
for(int i=lenBig-1;i>=0;i--){
int carry;
if((lenBig-i)<=lenSmall){
carry=(s1[i]+s2[i]-'0'-'0'+carryNum)/10;
}else{
if(s1.length()>s2.length()){
carry=(s1[i]-'0'+carryNum)/10;
}else{
carry=(s2[i]-'0'+carryNum)/10;
}
if(carry<1){
break;
}
}
carryTime+=carry;
carryNum=carry;
}
return carryTime;
}
int main(int argc, char *argv[]){
string s1, s2;
while(cin>>s1>>s2, s1!="0"||s2!="0"){
int carryTime=stringAdd(s1,s2);
if(carryTime==0){
cout<<"No carry operation."<<endl;
}else if(carryTime==1){
cout<<carryTime<<" carry operation."<<endl;
}else{
cout<<carryTime<<" carry operations."<<endl;
}
}
return 0;
}
POJ-2562(用例能过,但一直WA)
最新推荐文章于 2022-02-16 22:36:48 发布