题解 | #火星A+B#
火星A+B
https://www.nowcoder.com/practice/46bb070835e548678c78477f1fb0b92e
#include<iostream> #include<string> #include<vector> using namespace std; int length(string str){ int k=1; for (int i = 0; i < str.size(); i++){ if (str[i] == ','){ k++; } } return k; } int main(){ int arr[25] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 }; string str1, str2; while (cin >> str1 >> str2){ vector<int> vec1; vector<int>vec2; int l1 = length(str1); int l2 = length(str2); if (l1 > l2){ for (int i = 0; i < l1 - l2; i++){ vec2.push_back(0); } } else{ for (int i = 0; i < l2 - l1; i++){ vec1.push_back(0); } }int m; for (int i = 0; i < str1.size(); ){ if (str1[i] != ','){ if (str1[i + 1] == ','||i+1==str1.size()){ m = str1[i] - '0'; vec1.push_back(m); i++; } else{ m = (str1[i] - '0') * 10 + str1[i + 1] - '0'; vec1.push_back(m); i = i + 2; } } else{ i++; } } for (int i = 0; i < str2.size();){ if (str2[i] != ','){ if (str2[i + 1] == ','||i + 1 == str2.size()){ m = str2[i] - '0'; vec2.push_back(m); i++; } else{ m = (str2[i] - '0') * 10 + str2[i + 1] - '0'; vec2.push_back(m); i = i + 2; } } else{ i++; } } int x=0, y=0; for (int i = vec1.size()-1; i >= 0; i--){ vec1[i] = vec1[i] + vec2[i]+x; x = vec1[i] / arr[y]; vec1[i] = vec1[i] % arr[y]; y++; } if (x != 0){ vec1.insert(vec1.begin(), 1); } printf("%d", vec1[0]); for (int i = 1; i < vec1.size(); i++){ printf(",%d", vec1[i]); } printf("\n"); } }