在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
结论
如果您对本文有任何疑问,请在评论部分中提及它们。
热门推荐
1 泰迪狗狗如何训练
3 狗狗训练图
5 狗狗训练方法
6 训练狗狗不咬人
8 敏捷训练狗狗
10 训练狗狗吃饭
11 泰迪狗狗训练
12 狗狗从多大开始训练
13 狗狗训练学校费用
14 怎么去训练狗狗
15 狗狗训练狗狗
16 狗狗训练教程
17 训练狗狗装死
18 泰迪狗狗的训练方法