Home Pat 1477 计算数字和
Post
Cancel

Pat 1477 计算数字和

题目

给定一个非负整数 N,你的任务是计算 N 的所有数字的总和,并以英语输出总和的每个数字。

输入格式

共一行,包含一个整数 N。

输出格式

共一行,用英语输出总和的每个数字,单词之间用空格隔开。

输入样例:

1
12345

输出样例:

1
one five


题解

  1. 数字单词先缓存,制表。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
     #include <iostream>
     using namespace std;
        
     int main()
     {
         string n;
         cin >> n;
         int sum = 0;
         for (auto ch : n) {
             sum += ch - '0';
         }
         string s_sum = to_string(sum);
         string word[10] = {"zero", "one", "two", "three", "four", "five","six", "seven", "eight", "nine"};
            
         for (int i = 0; i < s_sum.size(); ++i ) {
             cout << word[s_sum[i] - '0'];
             if (i != s_sum.size() - 1) {
                 cout << ' ';
             }
         }
         return 0;
     }
    
This post is licensed under CC BY 4.0 by the author.
Contents