思路:
一个string读进来,挨个转换成int加起来,再一位一位取出来输出。
我用的stack输出,用数组啊vector啊啥的都没问题,注意输出顺序就好。
另外要注意的一点是,假如sum = 0,要特判一下,让程序输出zero,什么都不输出有一个点会过不去。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define FOR(i,a,n) for (int i=a;i<n;i++)
#define FRO(i,n,a) for (int i=n;i>a;i--)
#define mst(a,i) memset(a,i,sizeof a)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi pair<int,int>
#define eps 1e-6
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
const int mod = 1000000007;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
const int N = 1e5+6;
// const int MAXN=1e4;
// int a[N]={};
// int b[N]={};
int main(){
string s;
cin >> s;
string ch[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
ll sum = 0;
for(int i = 0 ; i < s.size() ; i++)
{
sum += (s[i] - '0');
}
stack<int> stk;
while(sum)
{
stk.push(sum % 10);
sum /= 10;
}
if(stk.size() == 0)cout << ch[0];
else
{
while(!stk.empty())
{
cout << ch[stk.top()];
if(stk.size() != 1)cout << ' ';
stk.pop();
}
}
return 0;
}
``