1.
#99乘法表#第一种 for i in range(1,10): s=‘‘ for j in range (1,i+1): s+=str(j)+‘*‘+str(i)+‘=‘+str(j*i)+‘\t‘ print s #第二种
for i in range(1,10):
s=‘‘
for j in range (1,i+1):
s+=str(j)+‘*‘+str(i)+‘=‘+‘{0:2d}‘.format(j*i)+‘\t‘ #{0:2d}解释:表示0 代表j*i,:2d 表示两个宽度的10进制数显示
print s
2.#1-100的素数 num = 2 cont = 0 while num < 100: flag = 1 i = 2 while i < num : if num % i == 0: flag=0 break i=i+1 if flag == 1: cont =cont + 1 print num num = num + 1 print ‘素数的个数是%d‘ %(cont)
3. #m=1*2*3*4*...n 求m为不大于20000时的最大n m = 1 n = 1 while m <=20000: m = m * n n+=1 n= n-1 print n
4.#1-30内的 勾股数 for a in range (1,30): for b in range (1,30): for c in range(1,30): if a * a + b * b == c *c: print a,b,c
5.#寻找平方后三位与自身一样的数字(仅限三位数) n = 100 while n <= 1000: m = n * n if m % 1000==n: print n n=n+1
时间: 2025-01-01 21:03:52