也是一道相对简单的题目,这道题容易出错的点是字符串与整型的转换:
for(int i = 1; i < re.length(); i++){
cout << " " << op[re[i] - '0'];
}
不要忘记op[re[i]] - '0'
。
using namespace std;
int main() {
string s, re;
string op[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
cin >> s;
int t = 0;
for(int i = 0; i < s.length(); i++){
t += (s[i] - '0');
}
re = to_string(t);
cout << op[re[0] - '0'];
for(int i = 1; i < re.length(); i++){
cout << " " << op[re[i] - '0'];
}
return 0;
}
网友评论