python
输入a和n,求s=a+aa+aaa+...na..+...,a表示数值,n表示数量,如a=2,n=3,则求出2+22+222的值刚接触python,求大佬解答 本帖最后由 bluerabbit 于 2020-11-23 21:47 编辑def fs(a, n):
s = 0
for i in range(1, n+1):
s += int(str(a)*i)
print(s)
>>> fs(2,3)
246
>>> fs(1,2)
12
>>> fs(3,6)
370368 a = int(input())
n = int(input())
sum =0
for i in range(n)
sum = sum +a*(i+1)*pow(10,n-(i+1))
望采纳! for(int num=1; num<=n; num++) {
tmp=(tmp*10)+a
sum+=tmp
} 本帖最后由 overlord012 于 2020-11-23 22:37 编辑
def sum(a, n):
count = 0
max_num = 0
for max_index in range(n):
max_num += a * pow(10, max_index)#先求出累加和中的最大值
for add_index in range(n):
count += max_num
max_num = int(max_num/10)#按序累加到最小的一位数数字
return count
a = int(input("please enter a: "))
n = int(input("please enter n: "))
print(sum(a, n)) # 定义数值
num = input("请输入一个数字:>>>")
# 定义数量
times = int(input("请输入次数:>>>"))
# 定义空字符串
s = ""
# 定义默认计算结果
sum = 0
# 遍历数量
for i in range(1, times):
# 字符串重复
s = num * i
# 强转结果并累加
# 相当于sum = sum + int(s)
sum += int(s)
# 输出最后结果
print(sum) a = input('请输入1~9之间的整数:\n')
n = int(input('请输入1~100之间的整数:\n'))
num = []
for i in range(1, n+1):
num.append(int(a * i))
print(sum(num))
print(num) a,n=int(input()),int(input())
print(sum())
a,n=int(input()),int(input())
print(sum()) 感谢各位大佬,我会慢慢研究各位诗一样的代码
页:
[1]