因為條件會感覺很多。
實際上就是先把輸入的數字每一位數存放於陣列每一格中,
再用一個for從高位數開始跑到低位數,
然後去判斷跑到的數字:
1.如果是1~9,就輸出它的值跟它的單位。
2.如果是0,而且是在萬位和億位,只要輸出它的單位即可。
3.如果是0,而且不在萬位和億位,但是比它低一位數的地方有數字,就輸出"零"。
4.非以上狀況,不輸出。
[C++](2ms, 730KB)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
int main( void ) | |
{ | |
string number[15] = { "零", "壹", "貳", "參", "肆", "伍", "陸", "柒", "捌", "玖" }; | |
string value[15] = { "拾", "億", "仟", "佰", "拾", "萬", "仟", "佰", "拾", "" }; | |
int num; | |
while( cin >> num ) | |
{ | |
int num_divide[15] = {0}; | |
int digit = 9; | |
while( num ) | |
{ | |
num_divide[digit--] = num % 10; | |
num /= 10; | |
} | |
for( int i = digit+1 ; i < 10 ; i++ ) | |
{ | |
if( num_divide[i] != 0 ) | |
cout << number[num_divide[i]] << value[i]; | |
else if( i == 1 || i == 5 ) | |
cout << value[i]; | |
else if( num_divide[i+1] != 0 ) | |
cout << number[num_divide[i]]; | |
} | |
cout << endl; | |
} | |
} |
0 意見:
張貼留言