1 #题目: Find the value of S=A+AA+AAA+AAAA+AA...A, where a is a number. For example 2+22+222+2222+22222 (there are 5 numbers added at this time), several numbers are added with keyboard control.
Code:
2 3 num = int (input (' Enter the number that needs to be added: ')) 4 m = int (input (' Please enter the number of times to add: ')) 5 s = 0 6 for j in Range (1,m+1): 7 for I in Range (1,J+1): 8 s + = num* (10** (i-1)) 9 print (s)
Operation Result:
[[email protected] code_100]# python code_18.py Please enter the number you want to add: 3 Enter the number of times you want to add: 537035[[email protected] code_100]# python Code_ 18.py Enter the number you want to add: 7 Enter the number of times you want to add: 886419746[[email protected] code_100]# python code_18.py Enter the number you want to add: 2 Enter the number of times you want to add: 824691356[[email protected] code_100]# python code_18.py Enter the number you want to add: 8 Enter the number of times you want to add: 296
Code Explanation:
2 3 num = int (input (' Enter the number to add: ')) 4 m = int (input (' Please enter the number of times to add: ')) 5 s = 0 6 for j in Range (1,m+1): #遍历所有 Number to add 7 for I in Range (1,j+1): #遍历每一个数字的个十百千 .... Bit 8 S + = num* (10** (i-1)) #将所有的数字循环相加 9 print (s) #打印结果
This article is from the "Learning Notes" blog, so be sure to keep this source http://netsyscode.blog.51cto.com/6965131/1747427
"Python" programming language Introduction Classic 100 cases--18