a = b = c = 0
time = 0
count=0
while a+b+c<30:
flag = 0
if a<10:
a = a+1
flag = 1
if (b<10) and (time%2==0):
b = b+1
flag = 1
if (c<10) and (time%4==0):
c = c+1
flag = 1
if flag:
count = count+1
time = time + 1
print(count)
n = 0
a = 0
b = 0
count = 0
while n<1000:
a = a+1
if a>20:
a = 1
b = b+1
if b>30:
b = 1
if a == b:
count = count+1
n = n+1
print(count)
340
练习:化功大法
编写一个程序,输入一个纯小数,然后将其转换成一个最简分数。
a = float(input("输入一个小数:"))
x=1
y=a
# 转化成整数
while abs(y-int(y)>1e-10):
x = 10*x
y = x*a
#找出x,y的最大公约数
i=y
while (i>0) and ((x%i!=0) or (y%i!=0)):
i = i-1
# 将x,y除以他们的最大公约数
y = int(y/i)
x = int(x/i)
print(str(y)+"/"+str(x))