1.Description:

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Notes:

  •  Each input file contains one test case. Each case occupies one line which contains an N (10100​​).1K10,0Nk<<N2<N1​​1000.

  • For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

2.Example:

Input:
12345
Output:
one five

3.solutions:

C Version:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 
 5 int main() {
 6     char digits[] = {};
 7     char NUMBERS[][10] = {"zero", "one", "two", "three", "four", "five","six",
 8                         "seven", "eight", "nine", "ten"};
 9     scanf("%s", digits);
10     int sum = 0;
11     unsigned i;
12     for (i = 0; digits != NULL && i < strlen(digits); ++i) {
13         sum += digits[i] - 48;  // '0'的Ascill码为48
14     }
15     char* output;
16     sprintf(output, "%d", sum);
17     unsigned j;
18     for (j = 0; output[j] != '\0'; ++j) {
19         if (j != strlen(output) - 1)
20             printf("%s ", NUMBERS[output[j] - 48]);
21         else
22             printf("%s", NUMBERS[output[j] - 48]);
23     }
24     return 0;
25 }

Note: 自己的电脑上没错,提交时运行错误!

C++ Version:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main() {
 5     string digits;
 6     string NUMBERS[] = {"zero", "one", "two", "three", "four", "five","six",
 7                         "seven", "eight", "nine", "ten"};
 8     getline(cin, digits);
 9     cout << digits << endl;
10     int sum = 0;
11     for (int i = 0; i < digits.size(); ++i) {
12         sum += int(digits[i]);
13     }
14     string output = to_string(sum);
15     for (int j = 0; j < output.size(); ++j) {
16         if (j != output.size() - 1)
17             cout << NUMBERS[int(output[j])] << " ";
18         else
19             cout << NUMBERS[int(output[j])];
20     }
21     return 0;
22 }

 

Java Version:

 1 import java.util.Scanner;
 2 
 3 /**
 4  * Created by sheepcore on 2020-02-28
 5  */
 6 
 7 public class P1005_Spell_It_Right {
 8     public static void main(String[] args) {
 9         String digits;
10         String NUMBERS[] = {"zero", "one", "two", "three", "four", "five","six",
11                 "seven", "eight", "nine", "ten"};
12         Scanner scan = new Scanner(System.in);
13         digits = scan.nextLine();
14         int sum = 0;
15         for (int i = 0; i < digits.length(); ++i) {
16             sum += Integer.parseInt(digits.charAt(i) + "");
17         }
18         String output = sum + "";
19         int idx;
20         for (int j = 0; j < output.length(); ++j) {
21             if (j != output.length() - 1){
22                 idx = Integer.parseInt(output.charAt(j) + "");
23                 System.out.print(NUMBERS[idx] + " ");
24             }
25             else {
26                 idx = Integer.parseInt(output.charAt(j) + "");
27                 System.out.print(NUMBERS[idx]);
28             }
29         }
30     }
31 }

 

Python Version:

 1 """
 2     created by sheepcore on 2020-2-28
 3 """
 4 
 5 if __name__ == "__main__":
 6     digits = input()
 7     NUMBERS = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
 8                'eight', 'nine', 'ten']
 9     i = 0
10     for ch in digits:  
11         i += eval(ch)
12     sum = str(i)
13     output = ""
14     for num in sum:
15         output += " " + str(NUMBERS[eval(num)])
16     print(output.lstrip(), end='')

 

4.summary:

掌握C、C++、Java、Python中字符串与数字之间的转换方法。

版权声明:本文为sheepcore原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/sheepcore/p/12375631.html