A_1005

1005 Spell It Right (20)(20 分)

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 (<= 10100).

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

分析:使用char或string类型存储数字,在转换一下求和就行

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string words[] = {"zero", "one", "two", "three", "four", "five", 
                        "six", "seven", "eight", "nine"};
    char str[10100];
    cin >> str;
    int len = strlen(str);
    if(len == 1 && str[0] == '0')
        cout << "zero" << endl;
    int i, sum = 0, cnt = 0;
    for(i = 0; i < len; i++)
        sum = sum + int(str[i]) - 48;
    int arr[10100];
    while(sum != 0){
        arr[cnt++] = sum%10;
        sum /= 10;
    }
    for(i = cnt - 1; i >= 0; i--){
        cout << words[arr[i]];
        if(i != 0)
            cout << " ";
    }
        
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,499评论 0 10
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,553评论 0 13
  • 想必着急的不过是通宵的游戏,不过是四级的成绩,还有2000多被骗的时候,最比不过就是影响毕业的毕业设计了,这天,不...
    无感态阅读 191评论 0 0
  • 2018年5月3日 周四 晴 晚上,宝贝写完作业,还待在书房没出来,进屋发现她正认真地用尺子在一张纸...
    大荔小太阳阅读 250评论 0 0
  • 页面关键词 页面描述 搜索引擎索引方式
    饥人谷_kindness阅读 316评论 1 2