Input a natural number n, calculate the sum of its digits, and write each digit of the sum in Chinese pinyin.

1. Calculate the sum of your numbers

Analysis: Because you do n’t know how many digits n is, you can only use  n = n / 10; n% 10 to count what each digit is.

 

 1 #include <stdio.h>
 2  
 3 int main()
 4 {
 5     int n;
 6     int sum=0;
 7     scanf("%d",&n);
 8     while(n>0)
 9     {
10         sum=sum+n%10;
11         n=n/10;
12     }
13     printf("%d",sum);
14     return 0;
15 }

Second, the advanced version:

Read in a positive integer  n, calculate the sum of its digits, and write each digit of the sum in Chinese pinyin.

Input format: Each test input contains 1 test case, which gives the value of the natural number  n. Here, ensure that  n is less than  1.

Output format: each digit of the sum of the digits of n is output in one line,  there is 1 space between the pinyin digits, but there is no space after the last pinyin digit in a line.

Sample input: 1234567890987654321123456789        Sample output:yi san wu

Analysis: Because the number is too large, only strings can be used. 
Note: string numbers are converted into numbers: -'0 '; terminator:' \ 0 '
 1 #include<stdio.h>
 2 #define N 100
 3 #define M 5
 4 int main()
 5 {
 6     int sum=0,i=0,j;
 7     char  hz[10][5]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
 8     char sr[N];
 9     int cs[M];
10     for(i=0;i<M;i++)
11     cs[i]=-1;
12     scanf("%s",sr);
13     i=0;
14     while(sr[i]!='\0')
15     {
16         sum+=sr[i]-'0';
17         i++;
18     }
19     j=0;
20     while(sum>0)
21     {
22         cs[j]=sum%10;
23         sum=sum/10;
24         j++;
25     }
26     for(i=M-1;i>=0;i--)
27     {
28         if(i==0)
29         {
30             printf("%s",hz[cs[i]]);
31             
32         }else{
33             printf("%s ",hz[cs[i]]);
34         }
35      } 
36     
37     return 0;
38 
39 }
 

 

 

Guess you like

Origin www.cnblogs.com/Zhuohome/p/12754207.html