标签:判断循环
1、打印100以内的斐波那契数列(这个数列从第3项开始,每一项都等于前两项之和)(1) 第一次实现:
a = 1
b = 1
sum = a + b
print(a)
print(b)
print(sum)
for j in range(i):
b = sum - b
sum = sum + b
for i in range(1,101):
if i == sum:
print(i)
(2)第二次实现:
a = 1
b = 1
print(a)
print(b)
for i in range(1,101):
if i == a + b:
print(i)
a = b
b = i
(3)第三次实现:
a = 0
b = 1
c = 1
while c:
c = a + b
if c > 100:
break
print(c)
a = b
b = c
(4)第四次实现:
a = 0
b = 1
while b < 100:
print(b)
a,b=b,a+b # a = b , b = a + b
(1)第一次实现:
a = 1
b = 1
sum = a + b
count = 0
for j in range(102):
b = sum - b
sum = sum + b
count += 1
if count == 98:
print(sum)
(2)第二次实现:
a = 1
b = 1
count = 2
while True:
sum = a + b
a = b
b = sum
count += 1
if count == 101:
break
print(sum)
(3)第三次实现:
a = 0
b = 1
count = 0
while True:
a,b=b,a+b
count += 1
if count == 100:
print(b)
break
(4)第四次实现:
a = 0
b = 1
count = 0
while True:
c = a + b
count += 1
a = b
b = c
if count == 100:
print(c)
break
(5)第五次实现:
a = 1
b = 1
for count in range(99):
a,b=b,a+b
else:
print(b)
(1)第一次实现:
a = int(input("请输入边长长度:"))
print(a*"*")
for i in range(a-2):
print("*"+(a-2)*" "+"*")
print(a*"*")
(2)优化实现:
n =int(input(">>>"))
for i in range(n):
if i == 0 or i == n-1:
print(n*"*")
else:
print("*"+" "*(n-2)+"*")
(1)第一次实现:重复进行阶乘的累加,效率不高
n = int(input(">>>"))
sum = 0
for i in range(1,n+1):
tmp = 1
for j in range(1,i+1):
tmp *= j
sum += tmp
print(sum)
(2)第二次实现:
n = int(input("please input number:"))
c = 1
sum = 0
for i in range(1,n+1):
c = i * c
sum += c
print(sum)
避开3,5,10,2的倍数,能被这些数整除的都不是质数
从2到n的开平方,以外的数都不用去计算
(1)第一次实现:
num = int(input(">>>"))
for i in range(2,num):
if num%i == 0:
print("这不是一个素数")
break
else:
print("这是一个素数")
(2)第二次实现:
num = int(input(">>>"))
for i in range(2,int((num**0.5)+1)):
if num % i == 0:
print("This is not a prime number")
break
else:
print("This is a prime number")
(1)第一次实现:
a = 3
b = 1
for i in range(4):
if b == 1:
print(a*" "+b*"*"+a*" ")
elif b == 3:
print(a*" "+b*"*"+a*" ")
elif b == 5:
print(a*" "+b*"*"+a*" ")
elif b == 7:
print(a*" "+b*"*"+a*" ")
a = a - 1
b = b + 2
for j in range(4):
a += 1
b -= 2
if b == 5:
print(a*" "+b*"*"+a*" ")
elif b == 3:
print(a*" "+b*"*"+a*" ")
elif b == 1:
print(a*" "+b*"*"+a*" ")
(2)第二次实现:
n = 7
for i in range(1,n+1,2):
a = int((n - i)/2)
print(a*" "+i*"*"+a*" ")
for i in range(5,0,-2):
a = int((n - i)/2)
print(a*" "+i*"*"+a*" ")
(3)第三次实现:对称性
n = int(input(">>>"))
for i in range(-n//2,n//2+1):
if i <= 0:
print(-(i)*" "+"*"*(n+i*2) )
else:
print(i*" "+"*"*(n-i*2))
可以写成三元运算符:
n = int(input(">>>"))
for i in range(-n//2,n//2+1):
print(-(i)*" "+"*"*(n+i*2)) if i <= 0 else print(i*" "+"*"*(n-i*2))
(4)第四次实现:abs()取绝对值
n = int(input(">>>"))
for i in range(-n//2,n//2+1):
print(abs(i)*" "+"*"*(n-2*abs(i)))
(5)第五次实现:
n =int(input(">>>"))
a = n//2+1
for i in range(n):
i += 1
if i<a:
x = a - i
else:
x = i - a
y = n - x*2
print(" "*x+"*"*y)
(6)第六次实现:考虑偶数打印的问题
num = int(input(">>>"))
if not num%2:
num += 1
b = num // 2
for i in range(-b,b+1):
print(" "*abs(i)+"*"*(num-2*abs(i)))
(7)第七种实现:考虑偶数的问题
while True:
num = int(input("Please enter an odd number,Otherwise re-enter!"))
if num % 2 !=0:
break
for i in range(-num//2,num//2+1):
print(" "*abs(i)+"*"*(num-2*abs(i)))
(1)第一次实现:
for i in range(1,10):
for j in range(1,i+1):
print(j,"x",i,"=",i*j,"\t",end="")
print()
(2)第二次实现:
for i in range(1,10):
for j in range(1,i+1):
print("{}*{}={}\t".format(j,i,i*j),end="")
print()
(3)第三次实现:
b = 1
while b:
for i in range(1,b+1):
print("{}*{}={}\t".format(i,b,i*b),end="")
print()
b += 1
if b == 10:
break
(4)第四次实现:将每一行的空格数合成一样的
for i in range(1,10):
for j in range(1,i+1):
if j>1 and j*i < 10:
print("{}*{}={}".format(j,i,i*j),end=" ")
else:
print("{}*{}={}".format(j,i,i*j),end=" ")
print()
(1)第一次实现:效率及其之差
import datetime
start = datetime.datetime.now()
count = 0
for i in range(2,100001):
for j in range(2,i):
if i!=j:
if i%j == 0:
break
else:
#print(i,"\t",end="")
count += 1
end = (datetime.datetime.now()-start).total_seconds()
print(end)
print(count)
时间:139.644859
(2)第二次实现:
import datetime
start = datetime.datetime.now()
count = 0
for i in range(2,100001):
for j in range(2,int((i**0.5)+1)):
if i%j == 0:
break
else:
#print(i,"\t",end="")
count += 1
end = (datetime.datetime.now()-start).total_seconds()
print(end)
print(count)
时间:0.552862``
(3)第三次实现:
import datetime
start = datetime.datetime.now()
count = 1
#print(2,"\t",end="")
for i in range(3,100001,2):
for j in range(2,int((i**0.5)+1)):
if i%j == 0:
break
else:
#print(i,"\t",end="")
count += 1
end = (datetime.datetime.now()-start).total_seconds()
print(end)
print(count)
时间:0.490323
(4)第四次实现:
import datetime
start = datetime.datetime.now()
count = 1
#print(2,end="\t")
for i in range(3,100001,2):
if i > 10 and i % 10 == 5:# 在所有大于10的质数中,个位数只有1,3,7,9
continue
for j in range(3,int(i**0.5+1),2):
if i % j == 0:
break
else:
count += 1
#print(i,end="\t")
end = (datetime.datetime.now() - start).total_seconds()
print(end)
print(count)
时间:0.285051
(5)第五次实现:考虑到与6相邻的性质
import datetime
start = datetime.datetime.now()
num = 100000
count = 2 # 2和3这两个质数先算进去
for i in range(5,num,2):
if i%6 != 1 and i%6 != 5:
continue
for j in range(5,int(i**0.5)+1,2):
if i % j == 0:
break
else:
count += 1
#print(i)
end = (datetime.datetime.now()-start).total_seconds()
print(end)
print(count)
时间:0.283728
(1)第一次实现:
i = 0
sum = 0
while True:
num = int(input("please input a number:"))
sum += num
i += 1
a = input("continue?(Y/N)")
if a == "N":
average = sum / i
print(average)
break
(2)第二次实现:
count = 0
sum = 0
while True:
num = int(input(">>>"))
if not num:
break
sum += num
count += 1
print(sum/count)
(3)第三次实现:
sum = 0
count = 0
while True:
num = input("please input a number,quit enter!")
if num == "":
break
else:
sum += int(num)
count += 1
print(sum/count)
(1) 第一次实现:
b = 3
for i in range(1,4):
print(b*" "+i*"*")
b -= 1
print(7*"*")
for i in range(3,0,-1):
print(3*" "+i*"*")
(2)第二次实现:
n = int(input(">>>"))
a = n // 2
for i in range(-a,a+1):
if i < 0:
print(-i*" "+"*"*(n-a+i))
elif i == 0:
print(n*"*")
else:
print(a*" "+"*"*(n-a-i))
(3)第三次实现:
n = int(input(">>>"))
a = n // 2
for i in range(-a,a+1):
if i < 0:
print(" "*abs(i)+"*"*((a+1)+i))
elif i > 0:
print(" "*a+"*"*((a+1)-i))
else:
print(n*"*")
(4)第四次实现:
num = int(input(">>>"))
n = num // 2
for i in range(n,-(n+1),-1):
if i > 0:
a = i
else:
a = n
if i == 0:
print(num*"*")
else:
print(" "*a+"*"*((n+1)-abs(i)))
猴子吃桃问题.猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个.第二天早上又将剩下的桃子吃掉一半,又多吃了一个.以后每天早上都吃了前一天剩下的一半零一个.到第十天早上在想吃时,就只剩一个桃子了.求第一天共摘了多少个桃子?
n = 1
for i in range(1,10):
n = 2*(n+1)
print(n)
(1) 第一次实现:
a=int(input(">>>"))
if a<0:
print("输入格式不正确,请输入一个不超过5位的数")
elif a<100000:
if a<10:
num
elif a<100:
print("是一个2位数")
elif a<1000:
print("是一个3位数")
elif a<10000:
print("是一个4位数")
else:
print("是一个5位数")
else:
print("请输入一个不超过5位的数")
(2)优化实现1:
num = int(input(">>>"))
count = 0
while True:
num = num // 10
count += 1
if num == 0:
break
print(count)
(3)优化实现2:折半查找
num = int(input("Please enter a number to judge:"))
if num >= 1000:
if num >= 10000:
print("five digits")
else:
print("four digits")
else:
if num >= 100:
print("three digits")
elif num >= 10:
print("two digits")
elif num >= 0:
print("one digit")
else:
print("The negative number does not support judgment for a while. Please try again:")
(1)第一次实现:
for i in range(10,0,-1):
print(i)
(1)第一次实现:
for i in range(10):
if i % 2 != 0:
continue
print(i)
(2)优化实现1:
for i in range(10):
if i % 2 == 0:
print(i)
(3)优化实现2:
for i in range(2,10,2):
print(i)
for i in range(10,0,-2):
print(i)
(1)第一次实现:
num = 54321
digits = 10000
count = 0
for i in range(5):
print(num // digits)
num = num % digits
digits = digits // 10
count += 1
print(count)
(2)优化实现
num = int(input(">>>"))
if num > 100000:
print("请输入一个5位以内的数!")
elif num >=1000:
if num >=10000:
val = 5
else:
val = 4
elif num > 0:
if num >=100:
val = 3
elif num > 10:
val = 2
else:
val = 1
else:
print("请不要输入负数!")
print(val)
for i in range(val,0,-1):
print(num // 10**(i-1))
num = num % (10**(i-1))
(1)第一次实现:
r = int(input(">>"))
p = 3.14
s = p * (r ** 2)
print(s)
c = 2 * p * r
print(c)
(2)优化实现:
r = int(input(">>>"))
print("s = " + str(3.14 * r * r))
print("c = " + str(2 * 3.14 * r))
(1)第一次实现:
num1 = int(input())
num2 = int(input())
if num1 > num2:
print(num1,num2)
else:
print(num2,num1)
在python中没有三目运算符,但是提供了三元表达式:
真值 if条件 else 假值
(2)优化实现:
num1 = int(input())
num2 = int(input())
print(num1,num2) if num1 > num2 else print(num2,num1)
(1)第一次实现:
newNum = 0
while True:
num = int(input(">>>"))
if num > newNum:
newNum = num
prompt = input("continue?(Y/N)")
if prompt == "N":
break
print(newNum)
(2)第二次实现:
num1 = int(input("please input first number:"))
n = int(input("You want to compare it several times."))
for i in range(n):
num2 = int(input("please input second number:"))
if num2 > num1:
num1 = num2 #将第一次比较后大的值给num1,依次输入,每次num1中存放的都是最大的值
print("max number is {}".format(num1))
(3)第三次实现:
x = int(input(">>>"))
z = 0
for i in range(x):
y = int(input(">>>"))
if not z: #第一次非0进入语句进行执行,将y的值赋值给z,这时z的值就不在是0,第二次进入非真为0,if第一个判断就不在执行,进行else的判断,将最大值放到z中。
z = y
elif y > z:
z = y
print(z)
(4)第四次实现:
num1 = int(input("please input first number:"))
max = 0
while True:
num2 = input("please input second number,exit press enter!")
if num2:
num2 = int(num2)
if max < num2:
max = num2
else:
break
print("max is",max)
sum = 0
for i in range(1,101,2):
sum += i
print("add =",sum)
(1)第一次实现:
for i in range(1,10):
print(" "*7*(i-1),end="")
for j in range(i,10):
flog = i*j
if flog < 10:
end = " "
else:
end = " "
print(str(j) + "*" + str(i) + "=" + str(j*i),end = end)
print()
(2)第二次实现:
for i in range(1,10):
s = ""
for j in range(i,10):
s += "{}x{}={:<3} ".format(i,j,i*j)
print("{:>78}".format(s))
(3)第三次实现:将前三列的空格数和后面的保持一致
for i in range(1,10):
s = ""
for j in range(i,10):
if j <= 3:
s += "{}x{}={:<2} ".format(i,j,i*j)
else:
s += "{}x{}={:<3} ".format(i,j,i*j) # c风格:‘%d*%d=%2d ‘%(i,j,i*j)
print("{:>80}".format(s))
(4)第四次实现:也可以将三元表达式放在format函数中
for i in range(1,10):
s = ""
for j in range(i,10):
s += "{}x{}={:<{}} ".format(i,j,i*j,2 if j<=3 else 3)
print("{:>80}".format(s))
n = int(input(">>>"))
n = n // 2
for i in range(-n,n+1):
print(" "*(n-abs(i))+"*"*(abs(i)*2+1))
标签:判断循环
原文地址:http://blog.51cto.com/13502869/2090898