|
Title: Chinese Numbers
1000 ms time limit
Memory limit 32768 Kbytes
Problem Description
Enter an integer and output it in Chinese.
Enter
The input contains multiple lines, one integer per line (<= 2000000000).
Output
Represent the input integers in Chinese, one output per line.
Input sample
12345
87654321
10001
Sample output
Twelve thousand three hundred forty five
Eight hundred seven hundred sixty five thousand four hundred thirty two one
Ten thousand and one
My algorithm:
#include <stdio.h>
main ()
{
char * ch [11];
ch [0] = "\0"; ch [1] = "一"; ch [2] = "二"; ch [3] = "三"; ch [4] = "四";
ch [5] = "五"; ch [6] = "六"; ch [7] = "七"; ch [8] = "八"; ch [9] = "九"; ch [10] = "zero";
char * dw [6];
dw [2] = "十"; dw [3] = "百"; dw [0] = "千"; dw [5] = "万"; dw [4] = "billion"; dw [1] = "\0";
char number [11]; int last, n, m, i, j, have;
while (1)
{
scanf ("% s", number);
last = 1; have = 0;
for (i = 0; i <11&&number [i]! = '\0'; i ++);
for (j = 0; j <i; j ++)
{
n = number [j] -48;
m = i-j;
if (have == 0&&m> 4&&m <9&&n) have = 1;
if (last == 0&&n) printf (ch [10]);
printf (ch [n]); if (n) printf (dw [m% 4]);
if (m% 4 == 1)
{
if (m == 5&&have) printf (dw [5]);
if (m == 9) printf (dw [4]);
}
last = n;
}
printf ("\n");
}
}
As a result, the defendant's time was exceeded (the time limit is 1000 milliseconds), and I hope that the master can make a move. Thank you. |
|