# -*- coding: UTF-8 -*-
'''
题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时
共有5个数相加),几个数相加有键盘控制。
1.程序分析:关键是计算出每一项的值。
2.程序源代码:
'''
from functools import reduce
Tn = 0
Sn = []
n = int(input('n = :\n'))
a = int(input('a = :\n'))
for count in range(n):
Tn = Tn + a
a = a * 10
Sn.append(Tn)
print (Tn)
Sn = reduce(lambda x,y : x + y,Sn)
print (Sn)
NameError: name 'reduce' is not defined. 在python 3.0.0.0以后, reduce已经不在built-in function里了, 要用它就得from functools import reduce
C:
void py18()
{
int s=0,a,n,t;
printf("Please input a and n\n");
scanf("a=%d,n=%d",&a,&n);
t=a;
while(n>0)
{
s+=t;
a=a*10;
t+=a;
n--;
}
printf("a+aa+...=%d\n",s);
}
阅读(1229) | 评论(0) | 转发(0) |