题解 | Primary Arithmetic
Primary Arithmetic
https://www.nowcoder.com/practice/c1fb44e931394e6693671f49c899f5de
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
int add(vector<int>a, vector<int>b) {
int t = 0;
vector<int>c;
int count = 0;
for (int i = 0, j = 0; i <= a.size() || j < b.size() || t; i++, j++) {
if (i < a.size())t += a[i];
if (j < b.size())t += b[j];
c.push_back(t % 10);
t /= 10;
if (t > 0)count++;
}
return count;
}
int main()
{
string str1, str2;
while (cin >> str1 >> str2) {
if (str1 == "0" && str2 == "0")break;
vector<int>a, b;
for (int i = str1.size() - 1; i >= 0; i--)
a.push_back(str1[i]-'0');
for (int j = str2.size() - 1; j >= 0; j--)
b.push_back(str2[j]-'0');
int res = add(a, b);
if (res == 1)
printf("%d carry operation.\n", res);
else if(res>1)
printf("%d carry operations.\n", res);
else
printf("NO carry operation.\n");
}
}


