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
  • 题目的大致意思:输入一个非负整数,然后计算这个数的各个位置上数字的和,最后将和的各个位置上的数字以英文输出,输出格式是每两个英文之间有一个空格,行末不能有空格
    代码如下:
#include<iostream>
#include<string>
using namespace std;
int main()
{
    int flag=0,sum=0;
    string s;
    char book[12][10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
    cin>>s;
    for(int i=0;i<s.length();i++){
        sum=s[i]-'0'+sum;
    }
    string a=to_string(sum);
    for(int i=0;i<a.length();i++){
        if(!flag){
            cout<<book[a[i]-'0'];
            flag=1;
        }
        else{
            cout<<" "<<book[a[i]-'0'];
        }
    }
    return 0;
}
  • 思路:输入N的时候以字符串的方式进行输入,然后进行遍历每一个位置上的数,将每一位上的数类型转换为整型进行相加最后得到int类型的和sum,然后将sum进行类型转换为字符串的类型,然后进行遍历每一位上的数字,这里要设置一个数组,数组的下标为对应每一位上的数字,对应下标的数组元素为对应的英文。然后根据对sum的遍历每一位上的数字再进行对应英文输出即可。注意输出格式,这里采用一个标记flag来限制输出格式。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值