这道题是考查字符串的操作,需要注意的是PTA系统只能在c编译环境下使用gets接收一行字符串,在C++使用gets会出错。本题既可以用纯C做,也可以结合map来写,总体来说还是比较简单的。
版本2
#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <map>
using namespace std;
int main(){
string str;
cin>>str;
unordered_map<char, string> mp;
mp['1'] = "one";
mp['2'] = "two";
mp['3'] = "three";
mp['4'] = "four";
mp['5'] = "five";
mp['6'] = "six";
mp['7'] = "seven";
mp['8'] = "eight";
mp['9'] = "nine";
mp['0'] = "zero";
int sum = 0;
for(auto c: str){
sum += (c -'0');
}
str = to_string(sum);
for(int i = 0; i < str.size(); i++){
char c = str[i];
if(i==0) cout<<mp[c];
else cout<<" "<<mp[c];
}
return 0;
}
版本1
#include<cstdio>
#include<cstdlib> //有数字转字符串的函数,可以直接调用
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
int arr[100];
int k=0;
void change(int x){
do{
arr[k++]=x%10;
x=x/10;
}while(x!=0);
}
int main(){
string str;
map< int, string > mp;
// mp.insert(make_pair(0,"zero")); 另一种方法
mp[0]="zero";
mp[1]="one";
mp[2]="two";
mp[3]="three";
mp[4]="four";
mp[5]="five";
mp[6]="six";
mp[7]="seven";
mp[8]="eight";
mp[9]="nine";
cin>>str;
int sum=0;
for(int i=0;i<str.size();i++){
sum+=str[i]-'0';
}
change(sum);
for(int j=k-1;j>=0;j--){
cout<<mp[arr[j]];
if(j>0) printf(" ");
}
printf("\n");
return 0;
}