PAT 甲级 1005 Spell It Right
Posted zlrrrr
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 ? " " : " "); } return 0; }
以上是关于PAT 甲级 1005 Spell It Right的主要内容,如果未能解决你的问题,请参考以下文章
PAT 甲级 1005 Spell It Right (20 分)