题解
简单题。
格式错误,发现答案为1位数的情况考虑欠妥。
另外 字符串 的问题比较头大,学了一个学期的c++,用惯了string,结果c里没有string,尝试全部改为c++的cin,cout以后,出了点问题。c++里string 和 char 的关系比较复杂。
快忘了c怎么表达字符串数组了,这里因为就十个单词,偷懒了一下,采用了指针的做法。。。不知道有没有什么漏洞。
Source Code
#include <cstdio>
using namespace std;
int main()
{
char* numToEnglish[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
char tempNum;
int sum = 0;
int firstFlag = 1;
int oneDigitFlag = 1; // !!! format error
char* res[105];
int numOfDigits = 0;
scanf("%c", &tempNum);
while(tempNum != '\n'){
sum += tempNum - '0';
scanf("%c", &tempNum);
}
if(sum == 0){
printf("%s", numToEnglish[0]);
}
else{
while(sum != 0){
res[numOfDigits++] = numToEnglish[sum % 10];
sum /= 10;
}
while(--numOfDigits){
oneDigitFlag = 0;
if(firstFlag){
printf("%s", res[numOfDigits]);
firstFlag = 0;
}
else{
printf(" %s", res[numOfDigits]);
}
}
if(oneDigitFlag){
printf("%s", res[0]);
}
else{
printf(" %s", res[0]);
}
}
}
Original Problem
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
http://www.patest.cn/contests/pat-a-practise/1005