1005 Spell It Right (20 分)
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
作者:CHEN, Yue
单位:浙江大学
代码长度限制:16 KB
时间限制:400 ms
内存限制:64 MB
解题思路:
先求和计算,然后字符串转换为列表进行遍历。
代码:
num = input('')
sum_ = 0
for i in range(len(num)):
sum_ = sum_ + int(num[i])
lst = list(str(sum_))
for j in range(len(lst)):
if j != 0:
print(' ', end = '')
if lst[j] == '1':
print('one', end = '')
if lst[j] == '2':
print('two', end = '')
if lst[j] == '3':
print('three', end = '')
if lst[j] == '4':
print('four', end = '')
if lst[j] == '5':
print('five', end = '')
if lst[j] == '6':
print('six', end = '')
if lst[j] == '7':
print('seven', end = '')
if lst[j] == '8':
print('eight', end = '')
if lst[j] == '9':
print('nine', end = '')
if lst[j] == '0':
print('zero', end = '')
提交记录: