PAT答案(写出这个数)

题目链接

https://www.patest.cn/contests/pat-b-practise/1002

代码

#include
#include
#include
using namespace std;

int main() {
    string s1;
    stack<int> s2;
    int sum = 0, t;
    string a[10] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
    cin >> s1;
    for(int i=0; i < s1.size(); i++) sum += s1[i] - '0';
    while(sum) {
        s2.push(sum % 10);
        sum /= 10;
    }
    t = s2.size();
    while(--t) {
        cout << a[s2.top()] << " ";
        s2.pop();
    }
    cout << a[s2.top()];
    return 0;
}

你可能感兴趣的:(算法)