-
Notifications
You must be signed in to change notification settings - Fork 0
/
1005-Spell_It_Right .c
71 lines (54 loc) · 1.95 KB
/
1005-Spell_It_Right .c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
感谢:https://github.com/OliverLew/PAT/blob/master/PATAdvanced/1005.c
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.
Input Specification:
Each input file contains one test case. Each case occupies one line which contains an N (≤10^100 ).
Output Specification:
For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.
Sample Input:
12345
Sample Output:
one five
*/
//计算各位数的和sum,并用英语单词输出sum的各位数。
//输出用一个字符串数组,计算部分要注意能计算大数,低空间复杂度
//将输入看作字符串,使用getchar(),计算过程中转换计算
#include<stdio.h>
int main()
{
char a;
char eng_number[10][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
/*对比学习一下,大括号数组赋值可以换行
char c, *digits[] = {
"zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine"
};
*/
int sum = 0, digits[100], i = 0;
int flag = 0;
digits[0] = 0;
for(a = getchar(); a != '\n' && a != EOF; a = getchar()) //对比:while((c = getchar()) != '\n')
{
sum = sum + (int)a - '0';//(int)a 转换后为ASCII码,比真实值大'0'(48)
}
while(sum > 0)
{
digits[i++] = sum % 10;
sum = sum / 10;
flag = 1;
}
if(flag == 0) printf("%s", eng_number[0]); //注意输入为0的边界条件,学习更好的处理方法
for(i--; i >= 0; i--)
{
printf("%s", eng_number[digits[i]]);
if(i > 0) printf(" ");
}
/*对比:
if(sum >= 100)
printf("%s ", digits[sum / 100]);
if(sum >= 10)
printf("%s ", digits[sum % 100 / 10]);
printf("%s", digits[sum % 10]);
*/
return 0;
}