Write this number P1002 (Basic Level)

  As a second question, take the string just right. This question but I got stuck a little, or too weak. See title

  Title meaning is clear, is to let you add up every digital input, then the bits of the output alphabet. This question is a look int type to save any longlong not even think about 10 100 do not think so much, certainly array of characters. But alas, lost the next lost my home to forget about the 10 100 is the number of direct asserted that fit in the array. Thought to myself, I felt a draw 10 100  in respect of 10 100  bit ashamed ah. This is the largest array of stuff to open a 110 on the line, really is blind. This question is finished first difficulty, as summation, is very simple, so str [i] - '0' to obtain an integer, sets out to find out the cycle. Another difficulty is the desire of every cycle stop condition should be

if (0! = sum / 10 || 0! = sum% 10) // indispensable

  || behind after the addition to the prevention of just a few, is used to prevent the front sum is a multiple of 10. Finally talk about output, anyway, I switch case, can also be used if ...... else ......, casual. At present, I can not think of a better way.

Finally, of course, we are happy labeled AC codes friends

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int main()
 5 {
 6     char str[110];
 7     while (~scanf("%s", str))
 8     {
 9         int sum = 0;
10         for (int i = 0; str[i] != '\0'; i++)
11         {
12             sum += str[i] - '0' ;
13          }
 14          int i = 0 ;
15          while ( 0 ! = Sum% 10 || 0 ! = Sum / 10 )
 16          {
 17              str [i] = sum% 10 + ' 0 ' ;
18              i ++ ;
19              sum / = 10 ;
20          }
 21          str [i] = ' \ 0 ' ;
22          int len = strlen(str);
23         for (i = len - 1; i >= 0; i--)
24         {
25             switch (str[i] - '0')
26             {
27             case 0:
28                 printf("ling");
29                 break;
30             case 1:
31                 printf("yi");
32                 break;
33             case 2:
34                 printf("er");
35                 break;
36             case 3:
37                 printf("san");
38                 break;
39             case 4:
40                 printf("si");
41                 break;
42             case 5:
43                 printf("wu");
44                 break;
45             case 6:
46                 printf("liu");
47                 break;
48             case 7:
49                 printf("qi");
50                 break;
51             case 8:
52                 printf("ba");
53                 break;
54             case 9:
55                 printf("jiu");
56                 break;
57             }
58             if (i != 0)
59             {
60                 printf(" ");
61             }
62         }
63         putchar('\n');
64         getchar();
65     }
66     return 0;
67 }
View Code

  Oh, almost forgot the last, because the evaluation unit may be multiple sets of input so remember to use getchar () swallow Enter Oh!

Algorithm is not easy, gentlemen encourage each other

Guess you like

Origin www.cnblogs.com/daker-code/p/11616077.html