欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

day4 作业

程序员文章站 2022-07-14 17:27:32
...

作业1

numbers =1
for i in range(0,20):
    numbers*=2
print(numbers)

1048576
[Finished in 0.2s]

本程序的作用是输出2的20次方



作业2

summation = 0
num =1
while num<=100:
    if(num%3==0 or num%7==0) and num%21!=0:
        summation+=1
    num+=1
print(summation)

39
[Finished in 0.2s]

该程序的作用是输出0-100中能被3或7整除但不能被21整除的数

作业3:用for和while求1-100之间和与平均值

sum1 = 0
for x in range(1,101):
    sum1+=x
    x+=1
print(sum1,sum1/100)

sum1 = 0
x = 1
while x<=100:
    sum1+=x
    x+=1
print(sum1,sum1/100)

5050 50.5
5050 50.5
[Finished in 0.2s]

作业4:用for和while求1-100件能被3整除的和

sum1 = 0
for x in range(1,101):
    if x%3==0:
        sum1+=x
    x+=1
print(sum1)

sum1 = 0
x = 1
while x<=100:
    if x%3==0:
        sum1+=x
    x+=1
print(sum1)

1683
1683
[Finished in 0.2s]

作业5:用for和while求1-100件不能被7整除的和

sum1 = 0
for x in range(1,101):
    if x%7!=0:
        sum1+=x
    x+=1
print(sum1)

sum1 = 0
x = 1
while x<=100:
    if x%7!=0:
        sum1+=x
    x+=1
print(sum1)

4315
4315
[Finished in 0.2s]

作业6:


作业7:


作业8:

for x in range(100,1000):
    a = x%10
    b = x%100//10
    c = x//100
    if (a**3+b**3+c**3)==x:
        print('水仙花数:',x)
    x+=1
for x in range(100,1000):
    if (((x//100)**3)+((x%100//10)**3)+((x%10)**3))==x:
            print('水仙花数',x)
    x+=1

水仙花数: 153
水仙花数: 370
水仙花数: 371
水仙花数: 407
[Finished in 0.2s]

作业9:

x = 12345
index = 0
for index in range(len(str(x)),0,-1):
    str1 = str(x)
    print(str1[index-1])
print('该数是一个%d位数'%(len(str(x))))

print('该数是一个%d位数'%(len(str(x))))
5
4
3
2
1
该数是一个5位数
[Finished in 0.2s]