码迷,mamicode.com
首页 > 其他好文 > 详细

PAT Advanced 1005 Spell It Right

时间:2019-09-02 23:26:46      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:div   oid   spec   tor   include   extra   lse   pat   input   

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

标签:div   oid   spec   tor   include   extra   lse   pat   input   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!