当前位置:网站首页>周末作业-循环练习题(2)
周末作业-循环练习题(2)
2022-08-05 05:13:00 【非鱼丶丶】
- 判断101-200之间有多少个素数,并输出所有素数。
count = 0
for num in range(100, 201):
for x in range(2, int(num ** 0.5 +1)):
if num % x == 0:
break
else:
count += 1
print(num)
print(count)
- 求整数1~100的累加值,但要求跳过所有个位为3的数。
sum1 = 0
for x in range(1 , 100):
if x % 10 != 3:
sum1 += x
print(sum1)
- 有⼀分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的第20个分数
a = 2
b = 1
for x in range(1, 20):
a, b = a + b, a
print(a, '/', b, sep='')
- 写程序计算 n 的阶乘 n! 的结果
qua1 = 1
n = int(input('求n的阶乘'))
for x in range(1, n + 1):
qua1 *= x
print(qua1)
- 求1+2!+3!+…+20!的和
qua1 = 1
sum1 = 0
for n in range(1, 20):
qua1 *= n
sum1 += qua1
print(sum1)
# 方法2
sum1 = 0
for n in range(1, 20):
for x in range(1, n):
n *= x
sum1 += n
print(sum1)
写程序求表达式 a + aa + aaa + aaaa+ … 的结果,其中a是1~9的数字,求和的项数用n来控制。(a和n可以用变量来表示)
例如:a为3, n为5的时候: 3 + 33 + 333 + 3333 + 33333
n = int(input('n'))
a = int(input('a'))
i = 0
for x in range(1, n+1):
i = a * 10**(x - 1) + i
if x == n:
print(i)
else:
print(i, '+', sep='', end='')
# 方法2
n = int(input("n"))
a = input("a")
for x in range(1, n+1):
res = a * x
if x == n:
print(res)
else:
print(res, "+", sep="", end="")
控制台输出三角形
a.根据n的值的不同,输出相应的形状 n = 5时 n = 4 ***** **** **** *** *** ** ** * * b.根据n的值的不同,输出相应的形状(n为奇数) n = 5 n = 7 * * *** *** ***** ***** ******* c. 根据n的值的不同,输出相应的形状 n = 4 1 121 12321 1234321 n = 5 1 121 12321 1234321 123454321小明单位发了100元的购物卡,小明到超市买三类洗化用品,洗发水(15元),香皂(2元),牙刷(5元)。要把100元正好花掉,可有哪些购买结合?
for shampoo in range(1, 7):
for soap in range(1, 51):
for brush in range(1, 21):
if shampoo * 15 + soap * 2 + brush * 5 == 100:
print('洗发水', shampoo, '香皂', soap, '牙刷', brush)
- 一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)?
x = 0.00008
n = 0
while x <= 8848.13:
x = x * 2
n += 1
print(n)
- 古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
- 将一个正整数分解质因数。例如:输入90,打印出90=2x3x3x5。
num = int(input("整数"))
x = 2
print(num, '=', sep='', end='')
while num != 1:
if num % x == 0:
num = num / x
if num == 1:
print(x, sep='', end='')
else:
print(x, '*', sep='', end='')
else:
x += 1
- 某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。求输入的四位整数加密后的值
num =int(input("整数"))
a = num // 1000
b = num // 100 % 10
c = num % 100 // 10
d = num % 10
a = (a + 5) % 10
b = (b + 5) % 10
c = (c + 5) % 10
d = (d + 5) % 10
print(d, c, b, a, sep='')
# fang 2
num =input("整数")
total = ""
for item in str(num):
res = str((int(item)+5) % 10)
total = res + total
print(int(total))
- 本金10000元存入银行,年利率是千分之三。每过1年,将本金和利息相加作为新的本金。计算5年后,获得的本金是多少。
x = 10000
for i in range(1, 6):
x = x + x * 0.003
print(x)
- 输入一个整数,计算它各位上数字的和。(注意:输入的整数可以是任意位)
sum1 = 0
x = input('整数')
n = len(x)
for i in range(0, n):
sum1 += int(x[i])
print(sum1)
- 求两个数的最大公约数和最小公倍数。(提示:公约数一定小于等于两数中的小的那个数,且能同时被两个数整除;公倍数一定大于等于两数中的大数,且是大数的倍数又能被两数中的小数整除)
a = int(input("a"))
b = int(input("b"))
c = b if a >= b else a
# if a >= b:
# c = b
# else:
# c = a
for x in range(c, 0, -1):
if a % x == 0 and b % x == 0:
print(x)
res1 = a / x
res2 = b / x
res = res1 * res2 * x
print(res)
break
边栏推荐
猜你喜欢

jvm 三 之堆与栈

Flutter learning 2-dart learning
![[Go through 3] Convolution & Image Noise & Edge & Texture](/img/7b/2214020cadf06d9211fd40fb5f1b63.png)
[Go through 3] Convolution & Image Noise & Edge & Texture

Qt produces 18 frames of Cupid to express his love, is it your Cupid!!!

Dephi reverse tool Dede exports function name MAP and imports it into IDA

Dephi逆向工具Dede导出函数名MAP导入到IDA中
![[cesium] element highlighting](/img/99/504ca9802db83eb33bc6d91b34fa84.png)
[cesium] element highlighting

软件设计 实验四 桥接模式实验
![[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)](/img/86/9c9a2541f2b7089ae47e9832fffdb3.png)
[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)

Multi-threaded query results, add List collection
随机推荐
Difference between for..in and for..of
2022牛客多校第四场C.Easy Counting Problem(EGF+NTT)
Returned object not currently part of this pool
Dashboard Display | DataEase Look at China: Data Presents China's Capital Market
The software design experiment four bridge model experiment
shell函数
ES6 生成器
RL强化学习总结(一)
Cryptography Series: PEM and PKCS7, PKCS8, PKCS12
uboot enable debug printing information
The mall background management system based on Web design and implementation
物理层的接口有哪几个方面的特性?各包含些什么内容?
【cesium】Load and locate 3D Tileset
DOM及其应用
【过一下10】sklearn使用记录
Flutter Learning 4 - Basic UI Components
【Transfer】What is etcd
有用番茄来监督自己的同道中人吗?加一下我的自习室,一起加油
判断语句_switch与case
电话溥功能