PAT 1005. Spell It Right(20)
2018-01-16 09:58
323 查看
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
意思就是给一个非负整数,计算各位数字的和,例如:12345,计算1+2+3+4+5=15,再将所得和的各位用对应英语单词打印出来:one five
// 计算非负整数各位的和并用英语输出
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
//计算各位和
int calculate(int N) {
int a = 0;
while (N / 10) {
a += (N % 10);
N = N / 10;
}
a = a + N;
return a;
}
void compare(int i) {
switch (i) {
case 0:cout<<"zero"; break;
case 1:cout<< "one"; break;
case 2:cout<<"two"; break;
case 3:cout << "three"; break;
case 4:cout << "four"; break;
case 5:cout << "five"; break;
case 6:cout << "six"; break;
case 7:cout << "seven"; break;
case 8:cout << "eight"; break;
case 9:cout << "nine"; break;
default:return ; break;
}
}
//输出各位
void print(int M) {
int a[200], i = 0;
while (M / 10) {
a[i] = (M % 10);
i++;
M = M / 10;
}
compare(M);
i = i - 1;
for (; i >= 0; i--) {
cout << " ";
compare(a[i]);
}
}
int main()
{
int N, M;
cin >> N;
M = calculate(N);
print(M);
system("pause");
return 0;
}我感觉自己写的代码挺简单的,也比较容易懂,但是有两个测试点过不去,第4个和第7个测试点,还请大佬指教。
相关文章推荐
- PAT (Advanced Level) Practise - 1005. Spell It Right (20)
- 【PAT (Advanced Level)】1005. Spell It Right (20)
- PAT-A 1005. Spell It Right (20)
- 1005. Spell It Right (20)——PAT (Advanced Level) Practise
- PAT-1005 Spell It Right (20)
- PAT(Advanced Level)1005. Spell It Right (20) 栈 int转string
- PAT 1005 Spell It Right (20)(字符转换)
- 【PAT】【Advanced Level】1005. Spell It Right (20)
- PAT 甲级 1005. Spell It Right (20) 【字符串】
- 1005. Spell It Right (20)——PAT (Advanced Level) Practise
- [PAT]1005. Spell It Right (20)@Java
- PAT甲级-1005. Spell It Right (20)各位之和
- PAT(甲级)1005. Spell It Right (20)
- pat 1005. Spell It Right (20)
- 【PAT甲级】1005. Spell It Right (20)
- PAT (Advanced) 1005. Spell It Right (20)
- PAT(甲级)1005. Spell It Right (20)
- Python 实现Pat 1005. Spell It Right (20)
- PAT 1005. Spell It Right (20)
- PAT甲级1005.Spell It Right (20)