POJ 2562 Primary Arithmetic(简单题)

来源:互联网 发布:域名ip查询 编辑:程序博客网 时间:2024/06/05 00:47

【题意简述】:计算两数相加,有多少个进位。

【分析】:很简单,不过还是要注意输出的细节。当进位为1时,输出的operation,没有s。


详见代码:

// 216K 0Ms#include<iostream>using namespace std;int main(){int a,b;while(cin>>a>>b){if(a == 0&&b == 0) break; // 在这贡献了n次WA~,傻!int ans = 0;int tmp1,tmp2;int sum = 0;while(1){tmp1 = a%10;a /= 10;tmp2 = b%10;b /= 10;sum = tmp1 + tmp2 + sum;if(sum>=10){ans++;sum = 1;}elsesum = 0;if(a == 0&&b == 0) break;}if(ans == 0)cout<<"No carry operation."<<endl;else if(ans == 1)cout<<"1 carry operation."<<endl;elsecout<<ans<<" carry operations."<<endl;}return 0;}


0 0