当前位置:网站首页>One question per day - pat grade B 1002 questions

One question per day - pat grade B 1002 questions

2022-07-07 23:00:00 InfoQ

One 、PAT (Basic Level) Practice ( chinese ) The first 1002 topic

This question is very interesting , The topic is really simple , But for Python It examines all kinds of type How to calculate . Because it seems that I've been coding for a long time , It feels right , But put the code in pycharm I knew what was wrong when I read the newspaper last time , That character cannot be counted . Although it has wasted me a long time , But I also have a deeper understanding of data type calculation

At the end of the question chapter, there is a detailed type of repeated transformation , Go by yourself pycharm Just run and you'll know what's going on .

1002  Write this number  (20  branch )

Read in a positive integer  n, Calculate the sum of its figures , Write each digit of the sum in pinyin .

Input format :

Each test input contains  1  Test cases , That is, give the natural number  n  Value . Guaranteed here  n  Less than  10100.

Output format :

Output... In one line  n  Each of the digits of the sum of , There is... Between Pinyin numbers  1  Space , But there is no space after the last Pinyin number in the line .

sample input :

1234567890987654321123456789

sample output :

yi san wu

Ideas :

# The main idea of the topic : Give a number , Add up the numbers at each position , The number is turning into Chinese Pinyin
# First accept n,n Is a string form, that is str()
# hold n In tabular form list(), Add up each number , That is to return to str Pattern
# Become every number in the list , Match each number with a Chinese Pinyin
# So we have to build a dictionary , according to key matching value
# Because there are spaces in the output style, we need to add another loop , But pay attention to the deadline , The last one does not output “ ”, When equal to len( list ), Don't output spaces

Code :

transform={'1':'yi','2':'er','3':'san','4':'si','5':'wu','6':'liu','7':'qi','8':'ba','9':'jiu','0':'ling'}
# Input
a=input() 
# Convert the input numeric string to the form of a list
b=list(a)
 # Sum every digit in the number
c=sum(int(i) for i in b)
 # The sum result is converted to string form , Used to calculate the length
d=str(c)
 # Marker bit , Used to determine for The last of the loop
flag=0
for j in d:
 flag=flag+1
# If for The last bit of the loop , Then no spaces are output
 if flag==len(d):
 #print default end by end='\n', First modify it according to the situation
 print(transform[j],end='')
 else:
 print(transform[j],end=' ')

Detailed calculation process (pycharm function )

transform={'1':'yi','2':'er','3':'san','4':'si','5':'wu','6':'liu','7':'qi','8':'ba','9':'jiu','0':'ling'}
a=input()
print("input The type of input :",type(a))

b=list(a)
print(" Forward a list of b by :",b)

#c=sum(int(i) for i in b)
sum=0
for i in a:
 s=int(i)
 sum=sum+s
print('sum Data type of :',type(sum),"  result : ",sum)

d=str(sum)
print(" Convert to string form sum","  type :",type(d),"  Number in string form :",d,"  His length :",len(d))

flag=0
for j in d:
 flag=flag+1
 if flag==len(d):
 print(transform[j],end='')
 else:
 print(transform[j],end=' ')

原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207072036169923.html