知识点:字符串处理、数字拆分处理、模拟
法1)数字拆分处理 + 模拟
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
string s, res;
string c[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int num, t;
int main() {
cin >> s;
for (int i = 0; s[i]; i ++ ) num += s[i] - '0';
if(!num) res = c[0];
while (num) {
if(t) res = ' ' + res;
res = c[num % 10] + res;
num /= 10;
t ++;
}
cout << res << endl;
return 0;
}
字符串处理 + 模拟(看了y总的写法后写的,确实比原来的代码简化了,容易了)
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
string s, res;
string c[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int num, t;
int main() {
cin >> s;
for (auto x : s) num += x - '0';
res = to_string(num);
for (int i = 0; res[i]; i ++ ) {
cout << c[res[i] - '0'];
if(res[i + 1]) cout << ' ';
else cout << endl;
}
return 0;
}