PAT 1002. 写出这个数 (20)

c++实现

#include <iostream>

using namespace std;

char result[][10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};

void cal()
{
    string n;
    int sum = 0;
    string temp;
    int tmp = 0;

    //cout<< "please enter the value of n: ";

    cin>>n;
    //cout<< "n = " <<  n << endl;

    for(char ch : n)
    {
        sum += int(ch-48);
    }

    //cout << "sum = " << sum << endl;
    temp = to_string(sum);
    //cout << "temp = " << temp << endl;

    for(int i = 0; i< temp.length(); i++)
    {
        //cout << temp[i] << endl;
        tmp = int(temp[i]-48);
        //cout << tmp << endl;
        cout << result[tmp];
        //cout << i;
        if(i != temp.length()-1)
            cout << " ";
    }
}

int main()
{
    cal();
    return 0;
}

 

posted @ 2017-06-03 10:58  eyan422  阅读(104)  评论(0)    收藏  举报