首页 > 其他 > 详细

PAT Advanced 1005 Spell It Right

时间:2019-09-03 00:28:12      阅读:68      评论:0      收藏:0      [点我收藏+]

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 (≤).

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最多有101位,这里的add函数可以直接累加,不需要高精度加法。
#include <bits/stdc++.h>

using namespace std;

void add(vector<int> &digits,int b)
{
    int res=b;
    int ptr=0,len=digits.size();
    while(res!=0)
    {
        if(ptr<len)
        {
            digits[ptr]+=res;
        res=digits[ptr]/10;
        digits[ptr]%=10;
        ptr++;

        }else{
            digits.push_back(res);
            res=0;
        }
    }
}

int main()
{
    map<int,string> ans;
    ans[0]="zero";
    ans[1]="one";
    ans[2]="two";
    ans[3]="three";
    ans[4]="four";
    ans[5]="five";
    ans[6]="six";
    ans[7]="seven";
    ans[8]="eight";
    ans[9]="nine";
    string N;
    cin>>N;
    int len=N.length();
    vector<int> digits;
    digits.push_back(0);
    for(int i=len-1;i>=0;i--)
    {
        add(digits,N[i]-0);
    }
    len=digits.size();
    cout<<ans[digits[len-1]];
    for(int i=len-2;i>=0;i--)
        cout<< <<ans[digits[i]];
    cout<<endl;
    return 0;
}

 

PAT Advanced 1005 Spell It Right

原文:https://www.cnblogs.com/zest3k/p/11450462.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!