1005 Spell It Right (20分)

解法一:map

map<char,string> mp;

void init()
{
    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 main()
{
    init();

    string s;
    cin>>s;

    int sum=0;
    for(int i=0;i<s.size();i++) sum+=s[i]-'0';

    string res=to_string(sum);

    for(int i=0;i<res.size();i++)
    {
        if(i) cout<<' ';
        cout<<mp[res[i]];
    }

    //system("pause");
    return 0;
}

解法二:数组

string mp[10]={
"zero","one","two","three","four","five","six","seven","eight","nine"
};


int main()
{
    string s;
    cin>>s;

    int sum=0;
    for(int i=0;i<s.size();i++) sum+=s[i]-'0';

    string res=to_string(sum);

    for(int i=0;i<res.size();i++)
    {
        if(i) cout<<' ';
        cout<<mp[res[i]-'0'];
    }

    //system("pause");
    return 0;
}
posted @ 2021-01-02 15:16  Dazzling!  阅读(49)  评论(0编辑  收藏  举报