PAT 1005

  • 题目:
    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.
    Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N (≤10​100​​).
Output Specification:

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.
Sample Input:

12345

Sample Output:

one five

  • 解题思路
    1.将输入的整数存储入字符串中
    2.将每一个字符减去0的ASCLL值,转化为每一个整数的值,在将所有整数相加,存储进sum。
    3.将sum的每一位存储进整数数组A
    4.输出数组A对应的英语值

代码实现:

#include <iostream>
#include <string>
using namespace std;
char num[10][10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
int main()
{
    string nums;
    cin >> nums;
    int n = nums.length();
    int sum = 0;
    for(int i = 0; i < n; i++)
        sum += nums[i] -'0';
    int k = 0;
    int A[100];
   // cout <<sum << endl;
    do{
        A[k++] = sum % 10;
        sum = sum /10;
    }while(sum != 0);
    for(int i = k-1; i >0; i--){
        cout<< num[A[i]] << " ";
    }
    cout<< num[A[0]];
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiao1guaishou/article/details/88406268
今日推荐