題解
簡單題。
格式錯誤,發(fā)現(xiàn)答案為1位數(shù)的情況考慮欠妥。
另外 字符串 的問題比較頭大,學了一個學期的c++,用慣了string,結果c里沒有string,嘗試全部改為c++的cin,cout以后,出了點問題。c++里string 和 char 的關系比較復雜。
快忘了c怎么表達字符串數(shù)組了,這里因為就十個單詞,偷懶了一下,采用了指針的做法。。。不知道有沒有什么漏洞。
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