PAT 1002. 写出这个数 (20)
读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字。
输入格式:每个测试输入包含1个测试用例,即给出自然数n的值。这里保证n小于10100。
输出格式:在一行内输出n的各位数字之和的每一位,拼音数字间有1 空格,但一行中最后一个拼音数字后没有空格。
输入样例:
1234567890987654321123456789
输出样例:
yi san wu
1 #include<stdio.h> 2 #include<string.h> 3 int main(){ 4 char s[1000]; 5 scanf("%s",s); 6 int n = strlen(s); 7 int sum=0; 8 int i = 0; 9 while(n--) 10 { 11 sum = sum + s[i]-'0'; 12 i++; 13 } 14 i = 0; 15 int a[1000]; 16 while(sum) 17 { 18 a[i] = sum%10; 19 sum = sum/10; 20 i++; 21 } 22 char c[][9] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"}; 23 i--; 24 printf("%s",c[a[i]]); 25 while(i--) 26 { 27 printf(" %s",c[a[i]]); 28 } 29 }
python
1 a ,sum = input(),0 2 for i in a:sum += int(i) 3 b = ['ling','yi','er','san','si','wu','liu','qi','ba','jiu'] 4 c = [b[int(i)] for i in str(sum)] 5 print(c[0],end='') 6 for i in range(1,len(c)):print(' {0}'.format(c[i]),end='')