Each line of input contains two unsigned integers less than 10 digits.
For each line of input except the last you should compute and print the number of carry operations that would result from adding the two numbers, in the format shown below.
123 456 555 555 123 594 0 0
NO carry operation. 3 carry operations. 1 carry operation.
#include<iostream> (720)#include<string> using namespace std; string a,b; int main() { while(cin>>a>>b&&a!="0"&&b!="0") { int indexa=a.length()-1; int indexb=b.length()-1; int carry=0; int count=0; while(indexa>=0||indexb>=0) { if(a[indexa--]-'0'+b[indexb--]-'0'+carry>9) { carry=1; count++; } else carry=0; } if(count==0) cout<<"NO carry operation."<<endl; else if(count==1) cout<<count<<" carry operation."<<endl; else cout<<count<<" carry operations."<<endl; } return 0; }
//看了大家的题解,感觉大家方法都好高级,自己想的简单了点也是过了哈哈哈 //注意0进位,1进位,多进位的输出格式^_^ #include<iostream> using namespace std; int main() { int a,b; while(cin>>a>>b) { bool flag = false; //判断是否从右往左有进位 if(a==0&&b==0) break; int tmp1=0,tmp2=0,sum=0,cnt = 0; while(a>0||b>0) { tmp1 = a%10; tmp2 = b%10; cnt = tmp1+tmp2; if(flag==true) cnt =tmp1+tmp2+1; //若有进位 flag = false; //进位清空 if(cnt>=10) { flag = true; sum++; } a/=10; b/=10; } if(sum == 0) cout<<"NO carry operation."<<endl; else if(sum == 1) cout<<sum<<" carry operation."<<endl; else cout<<sum<<" carry operations."<<endl; } return 0; }
#include<stdio.h> #include<string.h> int main() { char a[100],b[100]; int c[100]={0},d[100]={0},i,num; scanf("%s%s",a,b); for(i=strlen(a)-1,num=0;i>=0;i--)//1.字符数组转整数数组第一位放个位容易计算 c[num++]=a[i]-'0'; for(i=strlen(b)-1,num=0;i>=0;i--) d[num++]=b[i]-'0'; int max= strlen(a)>strlen(b)?strlen(a):strlen(b); for(i=0,num=0;;i<max;i++)//2.解决进位问题 if(c[i]+d[i]>9)//需要进位 { num++;c[i+1]+=1; }//上一位加1 if(num==0) printf("NO carry operation.\n"); else if(num==1) printf("1 carry operation.\n"); else printf("%d carry operations.\n",num); }
#include <bits/stdc++.h> using namespace std; int main(){ string s1,s2; int a[110]= {0},b[110]= {0}; while(cin>>s1>>s2){ int flag=0; for(int i=0; i<s1.size(); i++) { a[i]=s1[s1.size()-i-1]-'0'; } int len=max(s1.size(),s2.size())+1; for(int i=0; i<len; i++) { if(i<s2.size()){ b[i]=s2[s2.size()-i-1]-'0'; } a[i]+=b[i]; if(a[i]>=10){ a[i+1]++; a[i]%=10; flag++; } } if(flag==0) cout<< "NO carry operation."<<endl; else if(flag==1)cout<< "1 carry operation."<<endl; else cout<<flag<<" carry operations."<<endl; } return 0; }
#include<iostream> #include<cstring> using namespace std; int main(){ char a[10],b[10]; while(cin>>a>>b){ int lenA=strlen(a); int lenB=strlen(b); int tempB=lenB-1; int count=0; int temp=0; for(int i=lenA-1;i>=0;i--){ if((a[i]-'0'+b[tempB--]-'0'+temp)>=10){ count++; temp=1; } else temp=0; } if(count==1) cout<<count<<" carry operation."<<endl; else if(count>1) cout<<count<<" carry operations."<<endl; else cout<<"NO carry operation."<<endl; } }
while True: try: a,b = input().split() if len(a) > len(b): a,b = b,a #保证a是比较短的 carry = count = 0 while a: #每次相加最后一位和进位,并在循环结尾去掉最后一位 if int(a[-1]) + int(b[-1]) + carry >= 10: carry = 1 count += 1 else: carry = 0 a = a[:-1] b = b[:-1] while b: if int(b[-1]) + carry >= 10: carry = 1 count += 1 else: carry = 0 b = b[:-1] if count == 0: print("NO carry operation.") elif count == 1: print("1 carry operation.") #注意输出细节 else: print('%d carry operations.' % count) except Exception: break
#include <iostream> #include <cstring> #include <algorithm> using namespace std; int main(){ char number1[12], number2[12]; while (cin >> number1 >> number2){ int len1 = strlen(number1); int len2 = strlen(number2); int carry[12]; int count = 0; memset(carry, 0, sizeof(carry)); for (int i = len1-1, j = len2-1; j >= 0 || i >= 0; j--, i--){ int number = number1[i] + number2[j] - '0' - '0' + carry[max(i, j) + 1]; if (number>=10){ carry[max(i, j)] = number / 10; count++; } } if (len1>len2){ for (int i = (len1 - len2) - 1; i >= 0; i--){ if (number1[i] + carry[i + 1] - '0'>=10){ carry[i] = ((number1[i] + carry[i + 1]-'0') / 10); count++; } } } else{ for (int i = (len2 - len1) - 1; i >= 0; i--){ if (number2[i] + carry[i + 1]-'0'>=10){ carry[i] = ((number2[i] + carry[i + 1]-'0') / 10); count++; } } } if (count > 1) cout << count << " carry operations."<<endl; else if (count == 0){ cout <<"No carry operations." << endl; }else cout << count << " carry operation." << endl; } }
__author__ = 'Yaicky' def calCarry(num1, num2, carry): carry = (num1 + num2 + carry) / 10 return carry while True: a, b = map(int, raw_input().strip().split()) if a==0 and b==0: break carry = count = 0 while a%10 or b%10 or a or b: #例842079407 799043235 不加上or a和or b会提前结束while carry = calCarry(a%10, b%10, carry) if carry: count += 1 a /= 10 b /= 10 if count == 0: print 'NO carry operation.' elif count == 1: print '1 carry operation.' else: print '%d carry operations.' % (count)
def fun(a): res = [] while a >0: t = a % 10 a = a / 10 res.append(t) return res def add(a, b): a = fun(a) b = fun(b) i, j = 0, 0 c = 0 while i < len(a) and j < len(b): if a[i] + b[j] >= 10: c+=1 i+=1 j+=1 return c while True: try: a, b = map(int, input().split()) if a == 0 and b == 0: break res = add(a, b) if res == 0: print("NO carry operation.") elif res == 1: print("%d carry operation."%(res)) else: print("%d carry operations."%(res)) except: break
#include <iostream> #include <string> using namespace std; string Reverse(string a){ string res; for(int i=a.size()-1; i>=0; --i){ res.push_back(a[i]); } return res; } int Add(string a, string b){ int temp = 0, cnt = 0; a = Reverse(a); b = Reverse(b); for(int i=0; i<a.size() || i<b.size(); ++i){ if(i<a.size()) temp+=a[i]-'0'; if(i<b.size()) temp+=b[i]-'0'; temp /= 10; if(temp) cnt++; } return cnt; } int main(){ string a, b; while(cin >> a >> b){ if(a == "0" && b == "0") break; int cnt = Add(a, b); if(cnt>1) cout << cnt << " carry operations." << endl; else if(cnt>0) cout << cnt << " carry operation." << endl; else cout << "NO carry operation." << endl; } }
#include <iostream> #include <istream> #include <string> using namespace std; const int MAXN = 10; struct MyInteger { //自定义整数类,用数组表示整数 int* digit; //数组表示数值各位 int length; //数组长度(位数) MyInteger(int x); MyInteger operator=(string str); }; MyInteger::MyInteger(int x) { digit = new int[MAXN](); *this = to_string(x); } MyInteger MyInteger::operator=(string str) { length = str.length(); for (int i = 0; i < length; i++) { digit[i] = str[length - i - 1] - '0'; } return *this; } //统计加法进位次数 int countCarry(const MyInteger& a, const MyInteger& b) { int count = 0, carry = 0; for (int i = 0; i <= a.length && i <= b.length && i < 10; i++) { carry = (a.digit[i] + b.digit[i] + carry) / 10; count += carry; } return count; } int main() { int a, b; while (cin >> a >> b && a && b) { int result = countCarry(MyInteger(a), MyInteger(b)); result == 0 ? cout << "NO" : cout << result; cout << " carry operation"; if (result > 1) { cout << "s"; } cout << "." << endl; } return 0; }
#include <iostream> using namespace std; int countCarry(string a, string b) { int cnt = 0, carry = 0; if (a.length() < b.length()) { swap(a, b); } int i, j; for (i = a.length() - 1, j = b.length() - 1; j >= 0; i--, j--) { carry = (carry + a[i] - '0' + b[j] - '0') / 10; if (carry) { cnt++; } } while (i >= 0) { carry = (carry + a[i] - '0') / 10; if (carry) { cnt++; } i--; } return cnt; } int main() { string a, b; int cnt; while (cin >> a >> b) { if (a == "0" && b == "0") { continue; } cnt = countCarry(a, b); if (cnt == 0) { cout << "NO carry operation." << endl; } else if (cnt == 1) { cout << "1 carry operation." << endl; } else { cout << cnt << " carry operations." << endl; } } return 0; }
#include<iostream> #include<cstdio> using namespace std; const int maxn = 15; int change(long a, int num[]) { int i = 0; while(a > 0) { num[i++] = a % 10; a /= 10; } return i; } int main() { long a, b; while(cin >> a >> b) { if(a == 0 && b == 0) break; int num1[maxn] = {0}, num2[maxn] = {0}; int lena = change(a, num1); int lenb = change(b, num2); int ans = 0, carry = 0; int i; for(i = 0; i < lena || i < lenb; ++i) { int temp = num1[i] + num2[i] + carry; if(temp >= 10) { carry = temp / 10; ans++; } else { carry = 0; } } if(ans == 0) printf("NO carry operation.\n"); else if(ans == 1) printf("1 carry operation.\n"); else printf("%d carry operations.\n", ans); } return 0; }
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <algorithm> using namespace std; int main(){ string a, b; int aa[15], bb[15]; while(cin >> a >> b){ if(a=="0" && b=="0") break; memset(aa, 0, sizeof(aa)); memset(bb, 0, sizeof(bb)); int alen = a.length(), blen = b.length(); for(int i = alen-1; i >= 0; i--) aa[alen-1-i] = a[i]-'0'; for(int i = blen-1; i >= 0; i--) bb[blen-1-i] = b[i]-'0'; int cnt = 0, c = 0, tmp = 0; for(int i = 0; i<alen || i<blen; i++){ tmp = aa[i]+bb[i]+c; c = tmp/10; if(c) cnt++; } if(cnt == 0) printf("NO carry operation.\n"); else if(cnt == 1) printf("1 carry operation.\n"); else printf("%d carry operations.\n", cnt); } return 0; }
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; void reverse(char str[]){ int len=strlen(str); for(int i=0;i<len/2;++i){ swap(str[i],str[len-i-1]); } } int main(){ char str1[12],str2[12]; while(scanf("%s %s",str1,str2)!=EOF){ int len=max(strlen(str1),strlen(str2)); reverse(str1); reverse(str2); int count=0; int carry=0; for(int i=0;i<len;++i){ carry=(str1[i]-'0'+str2[i]-'0'+carry)/10; if(carry==1) count++; } if(count==0){ printf("NO carry operation.\n"); }else if(count==1){ printf("%d carry operation.\n",count); } else{ printf("%d carry operations.\n",count); } } return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ string s1,s2; while(cin>>s1>>s2){ if(s1=="0"&&s2=="0")break; int len1=s1.length(),len2=s2.length(); int i=len1-1,j=len2-1,carry=0,c=0; while(i>=0||j>=0||carry>0){ int n1 = i>=0?s1[i]-'0':0; int n2 = j>=0?s2[j]-'0':0; if(carry+n1+n2>=10)c++; carry=(carry+n1+n2)/10; i--; j--; } if(c==0){ cout<<"NO carry operation."<<endl; }else if(c==1){ cout<<"1 carry operation."<<endl; }else{ cout<<c<<" carry operations."<<endl; } } return 0; }
#include<stdio.h> (737)#include<string.h> int main() { char a[11],b[11]; int cnt; while(scanf("%s %s",&a, &b)!=EOF) { if(a == 0 && b == 0) break; int len_a = strlen(a), len_b = strlen(b); cnt = 0; int k = 0, t, i = len_a-1, j = len_b-1; while( i > -1 || j > -1) { t = k; if(i > -1) t+=a[i--]-'0'; if(j > -1) t+=b[j--]-'0'; k = t/10; if(k == 1) cnt++; } if(cnt == 0) printf("NO carry operation.\n"); else if(cnt == 1) printf("%d carry operation.\n",cnt); else printf("%d carry operations.\n",cnt); } return 0; }
#include <bits/stdc++.h> using namespace std; int cmp(string x, string y){ int a[1001]; int b[1001]; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); int count=0; int x1=x.size(); int y1=y.size(); int m=0; for(int i=1001-x1;i<1001;++i){ a[i]=x[m++]-'0'; } m=0; for(int i=1001-y1;i<1001;++i){ b[i]=y[m++]-'0'; } if(x1<y1){ for(int i=1000;i>=1001-x1;--i){ b[i]=a[i]+b[i]; } for(int i=1000;i>0;--i){ if(b[i]>9){ count++; b[i-1]++; b[i]-=10; } } } else{ for(int i=1000;i>=1001-y1;--i){ a[i]=a[i]+b[i]; } for(int i=1000;i>0;--i){ if(a[i]>9){ count++; a[i-1]++; a[i]-=10; } } } return count; } int main(){ string x,y; while(cin>>x>>y){ if(x=="0"&&y=="0") break; int count = cmp(x,y); if(count==0) cout<<"NO carry operation."<<endl; else if(count==1) cout<<count<<" carry operation."<<endl; else cout<<count<<" carry operations."<<endl; } return 0; }