PAT-1005正しいスペル

負でない整数Nが与えられた場合、タスクはNのすべての桁の合計を計算し、合計のすべての桁を英語で出力することです。

入力仕様:

各入力ファイルには、1つのテストケースが含まれています。各ケースは、N(≤10100)を含む1行を占めます。

出力仕様:

テストケースごとに、合計の桁を英語の単語で1行に出力します。2つの連続する単語の間に1つのスペースが必要ですが、行末に余分なスペースがあってはなりません。

サンプル入力:

12345

サンプル出力:

one five

題名

文字列を指定して、各文字を追加し、結果の各数値を英語の文字に変換します

コード

#include<iostream>
#include<string>
using namespace std;

int main(){
    
    
    string n;
    cin>>n;
    string maptable[] = {
    
    "zero","one","two","three","four",
        "five","six","seven","eight","nine","ten"};
    int temp = 0;
    for(int i=0; i<n.size(); i++){
    
    
        temp += n[i]-'0';
    }
    //cout << temp<<endl;
    string res = to_string(temp);
    cout << maptable[res[0]-'0'];
    for(int i=1; i<res.size(); i++){
    
    
        cout <<" "<<maptable[res[i]-'0'];
    }
    return 0;
}

おすすめ

転載: blog.csdn.net/weixin_42100456/article/details/108742377
おすすめ