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

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;
}

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

时间: 2024-08-01 04:51:59

PAT Advanced 1005 Spell It Right的相关文章

PAT甲级1005 Spell It Right

题目:PAT甲级 1005 题解:水题.看到题目的第一时间就在想一位一位的mod,最后一加一转换就完事了.结果看到了N最大为10的100的次方,吓得我赶紧放弃这个想法... 发现碰到这种情况用字符串十分好用,这道题应该考察的就是这一点.大致思路就是把数字的每一位放到字符串中,然后通过ASCII码得到每一位的相加结果num,然后把num一位一位的放到stack中,使用stack是因为它先进先出的特性,最后输出就行了. 代码: 1 #include<cstdio> 2 #include<qu

PAT Advanced 1005

题目如下: 1005. Spell It Right (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 Specificat

PAT:1005. Spell It Right (20) AC

#include<stdio.h> #include<string.h> char alp[10][10]={"zero","one","two","three","four","five","six","seven","eight","nine"}; char str[1200];

PAT 甲级 1005 Spell It Right

https://pintia.cn/problem-sets/994805342720868352/problems/994805519074574336 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 c

1005. Spell It Right (20)——PAT (Advanced Level) Practise

题目信息: 1005. Spell It Right (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 Specificat

PAT 1005. Spell It Right

PAT 1005. Spell It Right (20) 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 li

1005. Spell It Right (20) -PAT

1005. Spell It Right (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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: E

PAT 1005. Spell It Right (20)

1005. Spell It Right (20) 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 w

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes