https://www.acwing.com/problem/content/1479/
水题。
得到sum后可每一次提取个位数字,或直接将其to_string。
解法一:
1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 string v[]={"zero","one","two","three","four","five","six","seven","eight","nine"}; 5 int main(void){ 6 string s; 7 cin>>s; 8 int sum=0; 9 for(int i=0;i<s.size();i++){ 10 sum+=s[i]-'0'; 11 } 12 vector<string> res; 13 if(sum==0){ 14 res.push_back(v[0]); 15 } 16 while(sum){ 17 res.push_back(v[sum%10]); 18 sum/=10; 19 } 20 reverse(res.begin(),res.end()); 21 for(int i=0;i<res.size();i++){ 22 cout<<res[i]<<" "; 23 } 24 return 0; 25 }
解法二:
1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 string v[]={"zero","one","two","three","four","five","six","seven","eight","nine"}; 5 int main(void){ 6 string s; 7 cin>>s; 8 int sum=0; 9 for(int i=0;i<s.size();i++){ 10 sum+=s[i]-'0'; 11 } 12 string n=to_string(sum); 13 for(auto x:n){ 14 cout<<v[x-'0']<<" "; 15 } 16 return 0; 17 }