甲级 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

题目意思很简单,就是对每一位数求和后得到的数用字母一位一位的表示即可,注意首位不可以为0

#include<iostream>
#include<string>
#include<map>
using namespace std;

map < int,string > m;

void init()
{
	m[1]="one";
	m[2]="two";
	m[3]="three";
	m[4]="four";
	m[5]="five";
	m[6]="six";
	m[7]="seven";
	m[8]="eight";
	m[9]="nine";
} 


void output(string str)
{
    int len=str.length();
    int flag=0;
    if(str=="0")
    {
        cout<<"zero";
        return ;
    }
    for(int i=0;i<len;i++)
    {
        if(i!=0&&str[i]=='0') 
        {
            cout<<" zero";
            continue;
        }
        if(flag==0)
        {
            cout<<m[str[i]-'0'];
            flag=1;
        }
        else cout<<" "<<m[str[i]-'0'];
    }
}

int main()
{
	init();
    string str;
    cin>>str;
    int sum;
    int len=str.length();
    for(int i=0;i<len;i++)
    {
        sum+=(str[i]-'0');
    }
    string s=to_string(sum);
    output(s);
    return 0;
}

其实我这写的初始化函数其实可以用一个数组存,不用map其实更加简单,注意一下0的处理,一般大整数都用string存就可以解决问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值