我可不是为了被全人类喜欢才活着的,只要对于某一|

王陸

园龄:6年11个月粉丝:2052关注:178

PAT 1005 Spell It Right 字符串处理

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
 
题目意思:对所给的一个非负整数n,求各位之和的每一位英文读音。
解题思路:先求所给n的各位之和sum,转换成string,输出对应的每一位读音即可。
 
复制代码
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
using namespace std;
int main()
{
    int i,sum=0;
    string str;
    string ans;
    string a[10]={"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    cin>>str;
    for(i=0; i<str.size(); i++)
    {
        sum+=str[i]-'0';
    }
    ans=to_string(sum);
    cout<<a[ans[0]-'0'];//第一个读音
    for(i=1;i<ans.size();i++)
    {
        cout<<" "<<a[ans[i]-'0'];
    }
    return 0;
}
复制代码

 

本文作者:王陸

本文链接:https://www.cnblogs.com/wkfvawl/p/11335745.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   王陸  阅读(214)  评论(0编辑  收藏  举报
历史上的今天:
2018-08-11 8月10号水题走一波-个人赛八
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起