当前位置:网站首页>周末作业-循环练习题(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
边栏推荐
- 【Untitled】
- Transformation 和 Action 常用算子
- 【过一下 17】pytorch 改写 keras
- Using QR codes to solve fixed asset management challenges
- 1.3 mysql batch insert data
- 第三讲 Gradient Tutorial梯度下降与随机梯度下降
- CAP+BASE
- 【过一下6】机器视觉视频 【过一下2被挤掉了】
- Develop a highly fault-tolerant distributed system
- What are the characteristics of the interface of the physical layer?What does each contain?
猜你喜欢

flex布局青蛙游戏通关攻略

DOM and its applications

第5讲 使用pytorch实现线性回归

类的底层机制
![[cesium] element highlighting](/img/99/504ca9802db83eb33bc6d91b34fa84.png)
[cesium] element highlighting
![[Study Notes Dish Dog Learning C] Classic Written Exam Questions of Dynamic Memory Management](/img/0b/f7d9205c616f7785519cf94853d37d.png)
[Study Notes Dish Dog Learning C] Classic Written Exam Questions of Dynamic Memory Management

第二讲 Linear Model 线性模型

The difference between span tag and p
![[Go through 3] Convolution & Image Noise & Edge & Texture](/img/7b/2214020cadf06d9211fd40fb5f1b63.png)
[Go through 3] Convolution & Image Noise & Edge & Texture

将照片形式的纸质公章转化为电子公章(不需要下载ps)
随机推荐
WPF中DataContext作用
分布式和集群
What are the characteristics of the interface of the physical layer?What does each contain?
redis 缓存清除策略
小白一枚各位大牛轻虐虐
数据库实验五 备份与恢复
server disk array
RDD和DataFrame和Dataset
UVA10827
多线程查询结果,添加List集合
Dashboard Display | DataEase Look at China: Data Presents China's Capital Market
LeetCode: 1403. Minimum subsequence in non-increasing order [greedy]
day10-字符串作业
redis复制机制
Flutter real machine running and simulator running
Convert the paper official seal in the form of a photo into an electronic official seal (no need to download ps)
Algorithms - ones and zeros (Kotlin)
Difference between for..in and for..of
Returned object not currently part of this pool
How can Flutter parent and child components receive click events