您的位置:首页 > 其它

PAT 1005. Spell It Right(20)

2018-01-16 09:58 323 查看


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 (<= 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个测试点,还请大佬指教。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: