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 contains one test case. Each case occupies one line which contains an N (<= 10^100^).

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 <bits/stdc++.h>
using namespace std;

char s[1111], num[1111];
char a[20][50] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

void itoa(int x) {
    if(x == 0) {
        num[0] = ‘0‘;
        num[1] = 0;
        return ;
    }
    stack<int> st;
    while(x) {
        st.push(x % 10);
        x = x / 10;
    }
    int sz = 0;
    while(!st.empty()) {
        num[sz++] = (char)(st.top() + ‘0‘);
        num[sz] = 0;
        st.pop();
    }
}

int main() {
    scanf("%s", s);
    int len = strlen(s);
    int sum = 0;
    for(int i = 0; i < len; i ++) {
        sum += s[i] - ‘0‘;
    }

    itoa(sum);
    int lenum = strlen(num);

    for(int i = 0; i < lenum; i ++) {
        printf("%s", a[num[i] - ‘0‘]);
        printf("%s", i != lenum - 1 ? " " : "\n");
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/zlrrrr/p/9420593.html

时间: 2024-07-29 11:10:07

PAT 甲级 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: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 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 (≤). Ou

PAT甲级——1005.SpellItRight(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 which contains an N (≤10 ?1

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

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 (20)(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