python2.7练习小例子(十七)

2018-06-18 02:37:32来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

    17):题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制。

    程序分析:关键是计算出每一项的值。

    程序源代码:

 

#!/usr/bin/python
# -*- coding: UTF-8 -*-

Tn = 0
Sn = []
n = int(raw_input('n = '))
a = int(raw_input('a = '))
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

    以上实例输出结果为:

 

n = 4
a = 4
4
44
444
4444
计算和为: 4936

    

#!/usr/bin/python
# -*- coding: UTF-8 -*-

a = int(raw_input("a:"))
n = int(raw_input("n:"))
list=[]
for i in range(1,n+1):
    list.append(int("{}".format(a)*i))
s = reduce(lambda x,y:x+y, list)
print list
print "计算和为:",s

    输出如下所示:

 

a:4
n:4
[4, 44, 444, 4444]
计算和为: 4936

    Python3 测试实例:

 

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

n = int(input('n = '))
a = int(input('a = '))
sum = 0
total = 0
for i in range(n):
    sum += (10 ** i)
    total += sum * a
print(total)

    

#usr/bin/env python3
#coding:utf-8

n = int(input('请输入层数:'))
a = int(input('请输入计算数:'))
Su = 0
Tu = []
for i in range(n):
    Tu.append(int(str(a)*(i+1)))
print('创建的数列为:%s' % Tu)
Su = sum(Tu)
for i in Tu:
    Su = Su + i
print('数列的和为:%s' % Su)

    

#!/usr/bin/python
# -*- coding: UTF-8 -*-

def fadd():
    a = input("输入数字:")
    loopNum = int(input("数字循环次数:"))
    sum = 0
    strsum  =""
    for i in range(loopNum):
        sum += int(str(a)*(i+1))
        strsum += str((str(a)*(i+1))) + "+"
    strsum = strsum[:len(strsum)-1] + "=" + str(sum)
    print strsum
fadd()

    

#!/usr/bin/python
# -*- coding: UTF-8 -*-

def calculate(a,n):
    sum = 0
    for i in range(1,n+1):
        x = int(str(a)*i)
        sum+=x
    print sum
calculate(4,4)

    

#!/usr/bin/python
# -*- coding: UTF-8 -*-

n = 4
c = 4

def num(n,time):
    '拼接数字,如 n =3,time = 4 返回3333'
    l = []
    while time > 0:
        l.append(str(n))
        time -= 1
    l = ''.join(l)
    return int(l)

sum = 0
for i in range(1,c + 1):
    j= num(n,i)
    sum = sum + j

print sum

    python3 测试实例:使用递归函数生成每一项的值,并相加:

 

#!/usr/bin/env python3

def f(n,a):
    if(n==1):
        return a
    else:
        r=f(n-1,a)*10+a
        return r

n=int(input("n=\n"))
a=int(input("a=\n"))
print("")
sum=0
for i in range(1,n+1):
    print(f(i,a))
    sum=sum+f(i,a)

print("sum=",sum)

    Python3 测试案例:

 

#!/usr/bin/python3

n=input('请输入计算数:')
m=eval(input('请输入层数:'))
s=0
for i in range(1,m+1):
    a=n*i
    s+=eval(a)
print('{}'.format(s))

    

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import math

a=input("请输入a:")
n=input("请输入数字的个数:")
r = 0
for i in range(0,int(n)):
    num=0
    for j in range(0,i+1):
        num += int(a) * int(math.pow(10,j))
    r += num
print(r)

    Python3 测试:

 

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

def suman(a, n):
    if n<1:    return []
    s = [a]
    m = 10**len(str(a))
    for i in range(0, n-1):
        s.append(s[i]*m + a)
    return sum(s), s

print(suman(4,5))

    Python3 下测试:

 

num=input('输入 1-10 之间的数字:')
times=int(input('次数:'))
s=0
for i in range(1,times):
    print(num*i,'+',end='')
    s+=int(num*i)
print(num*times,'=',s+int(num*times))

    Python3 下测试,检测输入是否为数字:

 

a=input('请输入一个整数:')
b=input('请输入次数:')
d=0

if str.isdigit(a)and str.isdigit(b):
    for i in range(1,int(b)+1):
        c=a*i
        d+=int(c)
    print('和为:'+str(d))
else:
    print('请重新输入!')

    Python3 测试:

 

for j in range(5):
    a = str(input("提示:请输入1~9之间的正整数:"))
    n = int(input("提示:请输入大于零的正整数:"))
    if n <= 0 or int(a) >= 10 or int(a) <= 0:
        print("请根据提示输入正确的数")       
        continue
    sum = 0
    for i in range(1, n+1):
        print(int(a*i), end = ' ')
        sum += int(a*i)
    print("这些数的总和为:%d"%sum)

    这个小例子对实际应用也是有帮助的。如果感觉不错的话,请多多点赞支持哦。。。

  原文链接:https://blog.csdn.net/luyaran/article/details/80063195

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:pip安装的模块在pycharm中导入

下一篇:day1_作业(账户登录检测)