1005. Spell It Right (20)

題解

簡單題。
格式錯誤,發(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

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 17,扁擔隊怎能沒我 年輕時候的我,長相還算對得起觀眾,如果說有什么不足之處,那肯定是有點駝背。當兵的時候,老兵總...
    草根情結閱讀 376評論 0 5
  • [em]e166[/em] 感賞宇宙賜予我力量!感賞宇宙總管大大對我有求必應,只要我保持純念,宇宙就會給我我想要的...
    zhzhtydnkshyb閱讀 294評論 0 1
  • 萬物皆拂過 生命的輪轉(zhuǎn) 老家拜年似乎總會少了人 想起自己去年傻傻的問候 一晃一輩子很快就會過去 難得知道自己想要什...
    黃土高坡上的風姑娘閱讀 136評論 0 0

友情鏈接更多精彩內(nèi)容