在Python程序中计算n + nn + nnn + u + n(m次)
在本教程中,我们将编写代码以找到n+nn+nnn+...+n(m次)的和。我们可以在Python中非常轻松地做到这一点。让我们看一些例子。
Input: n = 1 m = 5 Series: 1 + 11 + 111 + 1111 + 11111 Output: 12345
算法
请按照以下步骤解决问题。
1. Initialise the n and m. 2. Initialise total to 0. 3. Make the copy of n to generate next number in the series. 4. Iterate the loop m times. 4.1. Add n to the total. 4.2. Update the n with n * 10 + copy_n. 5. Print the total.
示例
请参见下面的代码。
# initializing n and m n = 1 m = 5 # initializing total to 0 total = 0 # making the copy of n to get next number in the series copy_n = n # iterating through the loop for i in range(m): # adding n to total total += n # updating n to get next number in the serias n = n * 10 + copy_n # printing the total print(total)
输出结果
如果运行上面的代码,您将得到以下结果。
12345
结论
如果您对本文有任何疑问,请在评论部分中提及它们。
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短