*** 千曲而后晓声,观万剑而后识器——————《文心雕龙》
2.习题 2.1.Demo1什么意思?用人话来说就是程序员是练出来的,不是看出来的,更不是空想出来的。只要有足够的代码量,连小盆友都能接项目,下面就是分享的一些基础习题。
""" 三、循环语句 Demo37 """ import random key = int(random.randint(0,101)) print("请猜测一个0~100的数(n结束)") while True: num = input("你的猜测:") if num == "n": print("结束") break num = int(num) if num == key: key2 = input("恭喜你猜对了!再来一把?y/nn") if key2 == "n": break else: if num < key: print("小了。") else: print("大了。")2.2.Demo2
""" 三、循环语句 Demo38 """ k = 1 flag = 0 print("找A , B 最大公约数:") num1 = int(input("A的值:")) num2 = int(input("B的值:")) if num1 > num2: num1,num2 = num2,num1 while k <= num1: if num1 % k == 0 or num2 % k ==0: flag = 1 ged = k k += 1 if flag == 1: print("A和B两数的最大公约数为:{}".format(ged)) else: print("A和B没有公约数。")2.3.Demo3
""" 三、循环语句 Demo39 """ import math import random num = 0 for i in range(1000000): x = random.random() * 2 - 1 y = random.random() * 2 - 1 if math.sqrt(x ** 2 + y ** 2) < 1: num += 1 print("PI为:", num * 4 / 1000000)2.4.Demo4
""" 三、循环语句 Demo40 """ num = float(input("请输入一个不为零的数:")) sum = 0 posi = 0 nega = 0 total = 0 while num != 0: sum += num if num < 0: nega += 1 else: posi += 1 total += 1 num = float(input("请输入一个不为零的数:")) print("正数:{}n".format(posi)) print("负数:{}n".format(nega)) print("总数:{}n".format(total)) print("平均数值:{}n".format(sum / total))2.5.Demo5
""" 三、循环语句 Demo41 """ num = int(input("请输入一个整数:")) if num < 2: print(" ", num, " n") while num > 1: i = 2 while num % i != 0: i += 1 print("{} ".format(i),end=" ") num /= i2.6.Demo6
""" 三、循环语句 Demo42 """ num = int(input("请输入一个数:")) for i in range(1, num + 1): k = i for j in range(num - i): print(end = " ") for j in range(1, i * 2): if j == 1 or j == i * 2: print("{}".format(i),end=" ") else: if j <= i: k -= 1 print("{}".format(k),end=" ") else: k += 1 print("{}".format(k),end=" ") print("")2.7.Demo7
""" 三、循环语句 Demo43 """ #模式一 # num = int(input("行数:")) # for i in range(num): # for j in range(i + 1): # print(j+1,end=" ") # print("") #模式二 # num = int(input("行数:")) # for i in range(num , 0 , -1): # for j in range(i): # print(j+1,end=" ") # print("") #模式三 # num = int(input("行数:")) # for i in range(num): # for j in range(num - (i + 1)): # print(end =" ") # for j in range(i + 1 , 0,-1): # print(j , end = " ") # print("") #模式四 num = int(input("行数:")) for i in range(num , 0, -1): for j in range(num - (i)): print(end =" ") for j in range(i): print(j + 1, end = " ") print("")2.8.Demo8
""" 三、循环语句 Demo44 """ num = int(input("行数:")) for i in range(num): for j in range(num - i -1): print(end = " ") for j in range(i * 2 - 1): if j == 0 or j == i * 2 - 2: print("1", end = " ") else: if j < i: print(2 ** j, end = " ") else: print(2 ** (i - j + 1),end = " ") print("")2.9.Demo9
""" 三、循环语句 Demo45 """ k = 0 for i in range(2,1001): flag = 1 for j in range(2,i): if i % j == 0: flag = 0 break if flag == 1: if i < 10: print(i, end = " ") elif i < 100: print(i, end = " ") else: print(i, end = " ") k += 1 if k % 8 == 0 and k != 0: print("")2.10.Demo10
""" 三、循环语句 Demo46 """ # i = 10000 # i = 20000 i = 30000 PI = 0 sum = 0 for j in range(1 , i + 1): sum += (-1) ** (j + 1) / (2 * j - 1) PI = sum * 4 print(PI)2.11.Demo11
""" 三、循环语句 Demo47 """ i = 10 # i = 20000 #i = 100000 e = 1 for j in range(1 , i + 1): item = 1 for k in range(1 , j + 1): item /= k e += item print(e)2.12.Demo12
""" 三、循环语句 Demo48 """ for i in range(2, 10000): sum = 0 for j in range(1,i): if i % j == 0: sum += j if sum == i: print(i,end = " ")2.13.Demo13
""" 三、循环语句 Demo49 """ import random k = ["剪刀","石头","布"] print("=" * 53) print(" " * 14, "剪刀石头布三局两胜制:") print("=" * 53) i = 0 me = 0 it = 0 while True: print("当前你赢了",me,"次n") print("当前电脑赢了",it,"次n") i = int(input("(1)剪刀 (2)石头 (3)布:n")) computer = k.index(random.choice(k)) print("计算机出",k[computer]) if i == computer + 1: print("平局!") else: if i == 1: if (computer + 1) == 2: print("电脑赢了这一把!") it += 1 else: print("你赢了这一把!") me += 1 elif i == 2: if (computer + 1) == 3: print("电脑赢了这一把!") it += 1 else: print("你赢了这一把!") me += 1 else: if (computer + 1) == 1: print("电脑赢了这一把!") it += 1 else: print("你赢了这一把!") me += 1 if me == 3 or it == 3: print("游戏结束!") if me == 3: print("胜利者是你") else: print("胜利者是电脑") break2.14.Demo14
""" 三、循环语句 Demo50 """ nums = [] max = 0 while True: num = int(input("输入一个数(0结束):")) nums.append(num) if num == 0: count = nums.count(max) break if max < num: max = num print("输入的数字中最大值为",max) print("输入的最大数字出现了",count,"次")2.15.Demo15
""" 三、循环语句 Demo51 """ bio = [] num = int(input("请输入一个十进制整数:")) while num > 0: bio += str(num % 2) num //= 2 bio.reverse() print("该数二进制表示为:") for i in range(len(bio)): print(bio[i],end="")2.16.Demo16
""" 三、循环语句 Demo52 """ six = [] num = int(input("请输入一个十进制整数:")) while num > 0: six += str(num % 16) num //= 16 six.reverse() print("该数16进制表示为:") for i in range(len(six)): print(six[i],end="")2.17.Demo17
""" 三、循环语句 Demo 53 """ import math import random #为了减少错误用自定义函数 ##求三角形面积 #已知3为(0,0) (1,0) (0,1) def s(ax,ay,bx,by,cx,cy): area = abs((ax * (by - cy) + bx * (cy - ay) + cx * (ay - by)) / 2.0) return area s0 = s(0,0,1,0,0,1) num = 0 for i in range(1000000): x = random.random() * 2 - 1 y = random.random() * 2 - 1 s1 = s(x,y,0,0,1,0) s2 = s(x,y,0,0,0,1) s3 = s(x,y,1,0,0,1) if x <= 0: num += 1 if (s1 + s2 + s3) == s0: num +=1 print("落到奇数区域概率为:", num / 1000000)2.18.Demo18
""" 三、循环语句 Demo54 """ k = 0 for i in range(2001,2100): if i % 400 == 0 or (i % 4 == 0 and i % 100 != 0): print(i,end=" ") k += 1 if k % 10 == 0 and k != 0: print("")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)