-
判断101-200之间有多少个素数,并输出所有素数。
count = 0 for x in range(101, 200): for y in range(2, x): if x % y == 0: break else: count += 1 print(x) print('有', count, '个素数')
-
求整数1~100的累加值,但要求跳过所有个位为3的数。
s = 0 for x in range(1, 101): if x % 10 == 3: continue else: s += x print(s)
-
有⼀分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的第20个分数
count = 1 numerator = 1 denominator = 2 while count < 20: count += 1 temp = denominator denominator = numerator + denominator numerator = temp print(denominator, '/', numerator)
-
写程序计算 n 的阶乘 n! 的结果
n = int(input('请输入n:')) result = 1 for x in range(1, n + 1): result *= x print(result)
-
求1+2!+3!+…+20!的和
s = 0 for x in range(1, 21): result = 1 for y in range(1, x + 1): result *= y s += result print(s)
-
写程序求表达式 a + aa + aaa + aaaa+ … 的结果,其中a是1~9的数字,求和的项数用n来控制。(a和n可以用变量来表示)
例如:a为3, n为5的时候: 3 + 33 + 333 + 3333 + 33333
a = int(input('请输入a:')) n = int(input('请输入n:')) s = 0 a1 = a for x in range(1, n + 1): print(a1) s += a1 a1 = a1 + a * (10 ** x) print(s)
-
控制台输出三角形
a.根据n的值的不同,输出相应的形状 n = 5时 n = 4 ***** **** **** *** *** ** ** * * n = int(input('请输入n:')) for x in range(n, 0, -1): for y in range(x): print('*', end='') print() b.根据n的值的不同,输出相应的形状(n为奇数) n = 5 n = 7 * * *** *** ***** ***** ******* n = int(input('请输入n(n为奇数):')) print((n + 1) // 2) for x in range(1, (n + 1) // 2 + 1): for y in range(1, n + 1): if y <= n // 2 - x + 1 or y >= n // 2 + x + 1: print(' ',end='') else: print('*',end='') print() c. 根据n的值的不同,输出相应的形状 n = 4 1 121 12321 1234321 n = 5 1 121 12321 1234321 123454321 n = int(input('请输入n:')) for x in range(1, n + 1): count = 1 for y in range(1, 2 * n): if y < n - x + 1 or y > n + x - 1: print(' ', end='') else: if y < n: print(count, end='') count += 1 else: print(count, end='') count -= 1 print()
-
小明单位发了100元的购物卡,小明到超市买三类洗化用品,洗发水(15元),香皂(2元),牙刷(5元)。要把100元正好花掉,可有哪些购买结合?
print('洗发水', '\t', '香皂', '\t', '牙刷') for soap in range(1, 50): for toothbrush in range(1, 20): # shampoo = (100 - soap * 2 - toothbrush * 5) / 15 for shampoo in range(1, 6): if shampoo * 15 + soap * 2 + toothbrush * 5 == 100: print(shampoo, '\t\t', soap, '\t\t', toothbrush)
-
一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)?
count = 0 paper_h = 0.08 mountain_h = 8848.13 * 1000 while paper_h < mountain_h: count += 1 paper_h = 0.08 * 2 ** count # print(count,'\t',paper_h) print(count)
-
古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
m0_r = 0 m1_r = 1 month = 1 rabbit = 1 n = int(input('请输入计算前多少个月的兔子对数:')) while month <= n: print('第', month, '个月有', rabbit, '只兔子') month += 1 rabbit = m0_r + m1_r m0_r = m1_r m1_r = rabbit
-
将一个正整数分解质因数。例如:输入90,打印出90=2x3x3x5。
num = int(input('请输入正整数:')) print(num, '= ', end='') from math import sqrt def is_prime(n): if n == 1: return False for i in range(2, int(sqrt(n)) + 1): if n % i == 0: return False return True while not is_prime(num): count = 2 for x in range(count, int(sqrt(num)) + 1): if is_prime(x) and num % x == 0: num = int(num / x) print(x, end='x') break else: print(num)
-
某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。求输入的四位整数加密后的值
data = int(input('请输入一个四位整数:')) a = (data // 1000 + 5) % 10 print(a) b = (data // 100 % 10 + 5) % 10 print(b) c = (data //10 % 10 + 5) % 10 print(c) d = (data % 10 + 5) % 10 print(d) a, d = d, a b, c = c, b print(a,b,c,d)
-
将一个正整数分解质因数。例如:输入90,打印出90=2x3x3x5。
-
本金10000元存入银行,年利率是千分之三。每过1年,将本金和利息相加作为新的本金。计算5年后,获得的本金是多少。
money = 10000 annual_interest_rate = 0.003 for x in range(5): interest = money * annual_interest_rate money += interest print(money)
-
输入一个整数,计算它各位上数字的和。(注意:输入的整数可以是任意位)
num = input('请输入一个整数:') s = 0 for x in num: temp = int(x) print(x) s += temp print(s)
-
求两个数的最大公约数和最小公倍数。(提示:公约数一定小于等于两数中的小的那个数,且能同时被两个数整除;公倍数一定大于等于两数中的大数,且是大数的倍数又能被两数中的小数整除)
a = int(input("请输入a:")) b = int(input("请输入b:")) big = a if a > b else b small = a if a < b else b for x in range(2,small): if a % x == 0 and b % x == 0: gcd = x print(gcd) for x in range(big, a * b + 1): if x % a == 0 and x % b == 0: lcm = x break print(lcm)
周末作业-循环练习题
最新推荐文章于 2023-06-25 20:12:28 发布