PAT 1002 Write out this number, read in a positive integer n, calculate the sum of its digits, and write each digit of the sum in Chinese pinyin.

topic:

Input format:

Each test input contains 1 test case, i.e. given the value of the natural number n. Here n is guaranteed to be less than 10100.

Output format:

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

Code:

1. Input operation

a = input()

2. Convert 0-9 into pinyin and store it in a list

lists_n = ["ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"]

3. Split the value we input into a value that can be calculated one by one, form a list, and store it with list_n

# 将字符串a用for去循环获取值,将获取的值存入列表。
list_n = []
for k in a:
    list_n.append(k)

4. Use another loop to calculate the sum between them

sum = 0
# 用j去获取列表list_n的值,用来相加。
for j in list_n:
    sum += int(j)
    # 等价
    # sum = sum + int(j)

5. Split the sum between them into a list, use it as an index, and take  the value in lists_n 

# 将最后的值转换成字符串,用for循环将他们的和,拆分成一个列表。
for i in str(sum):
    list.append(lists_n[int(i)])

6. The last step is output. The join() method is used to convert the list into a string output. The meaning of " " is to separate them with spaces

print(" ".join(list))

Full code:

a = input()
lists_n = ["ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"]
list_n = []
# 将字符串a用for去循环获取值,将获取的值存入列表。
for k in a:
    list_n.append(k)
# print(list_n)
sum = 0
# 用j去获取列表list_n的值,用来相加。
for j in list_n:
    sum += int(j)
    # 等价
    # sum = sum + int(j)
# print(sum)
list = []
# 将最后的值转换成字符串,用for循环将他们的和,拆分成一个列表。
for i in str(sum):
    list.append(lists_n[int(i)])
# print(list)
print(" ".join(list))

Guess you like

Origin blog.csdn.net/weixin_62854251/article/details/130201283