1005. Spell It Right (20)
2017-11-10 16:00
603 查看
题目:
Given a non-negative integer N, your task is to compute the sum of all the digi
4000
ts 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 (<= 10100).
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:
Sample Output:
题解:
简单的模拟题。
代码:
#include <cstdio>
#include <cstring>
using namespace std;
char word[][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int main()
{
char str[110];
scanf("%s", str);
int len = strlen(str), sum = 0;
for(int i = 0; i < len; i++) //计算出各位之和
sum += str[i] - '0';
int ans[5], k = 0;
while(sum) //将sum的每一位逆序保存在ans数组中
{
ans[k++] = sum % 10;
sum /= 10;
}
printf("%s", word[ans[--k]]);
for(int i = k - 1; i >= 0; i--)
printf(" %s", word[ans[i]]);
return 0;
}
Given a non-negative integer N, your task is to compute the sum of all the digi
4000
ts 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 (<= 10100).
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 <cstdio>
#include <cstring>
using namespace std;
char word[][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int main()
{
char str[110];
scanf("%s", str);
int len = strlen(str), sum = 0;
for(int i = 0; i < len; i++) //计算出各位之和
sum += str[i] - '0';
int ans[5], k = 0;
while(sum) //将sum的每一位逆序保存在ans数组中
{
ans[k++] = sum % 10;
sum /= 10;
}
printf("%s", word[ans[--k]]);
for(int i = k - 1; i >= 0; i--)
printf(" %s", word[ans[i]]);
return 0;
}
相关文章推荐
- PAT (Advanced Level) 1005. Spell It Right (20) 解题报告
- 1005. Spell It Right (20)
- 1005. Spell It Right (20)
- PAT (Advanced Level) 1005. Spell It Right (20) string转int,哈希
- 1005. Spell It Right (20)
- PAT---1005. Spell It Right (20)
- 1005. Spell It Right (20)
- PAT 1005. Spell It Right(20)
- PAT-PAT (Advanced Level) Practise 1005. Spell It Right (20) (简单模拟题)【一星级】
- 1005. Spell It Right (20)
- PAT1005. Spell It Right (20)
- PAT(甲级)1005. Spell It Right (20)
- 1005 Spell It Right (20)
- 1005. Spell It Right (20)(C++)
- 1005. Spell It Right (20)
- 1005. Spell It Right (20)
- 1005 Spell It Right (20)
- 1005. Spell It Right (20)
- 1005. Spell It Right (20)
- 【PAT (Advanced Level)】1005. Spell It Right (20)