本文共 7618 字,大约阅读时间需要 25 分钟。
for i in range(10): if not i & 0x01: print(i)
a = int(input("Please enter a numer: "))if a < 0: print('Error')if a < 10: print(1)elif a < 100: print(2)elif a < 1000: print(3)elif a < 10000: print(4)elif a < 100000: print(5)else: print('Error')
a=int(input("Please enter a numer: "))if a < 0: print('Error')elif a < 1000: if 9 < a < 100: print(2) elif a < 10: print(1) else: print(3)elif a < 10000: print(4)elif a < 100000: print(5)else: print('Error')
a = str(int(input("Please enter a number: ")))print(len(a))
a = int(input("Please enter a number: "))length = 0if a < 0: print('Error')elif a < 1000: if 9 < a < 100: length = 2 elif a < 10: length = 1 else: length = 3elif a < 10000: length = 4elif a < 100000: length = 5else: print('Error')print(length)for i in range(length): print(a % 10) a = a // 10
a = input("Please enter a number: ")b = int(a)print(len(a))for i in range(len(a)): print(b % 10) b = b // 10
a = int(input("Please enter a number: "))length = 0if a < 0: print('Error')elif a < 1000: if 9 < a < 100: length = 2 elif a < 10: length = 1 else: length = 3elif a < 10000: length = 4elif a < 100000: length = 5else: print('Error')print(length)for i in range(length): print(a - (a // 10) * 10) a = a // 10
a=int(input("Please enter a number: "))a *= 10while (a // 10) != 0: a //= 10 print(a % 10)
a=input('Please enter a number: ')b=int(a)for i in range(len(a),0,-1): te = 10 ** (i - 1) c = b // te print(c) b -= (c * te)print(len(a))
a = int(input("Please enter a number: "))length = 0if a < 0: print('Error')elif a < 1000: if 9 < a < 100: length = 2 elif a < 10: length = 1 else: length = 3elif a < 10000: length = 4elif a < 100000: length = 5else: print('Error')print(length)pre = 0for i in range(length,0,-1): cur = a // (10 ** (i - 1)) print(cur - pre * 10) pre = cur
a = int(input("Please enter a number: "))length = 0if a < 0: print('Error')elif a < 1000: if 9 < a < 100: length = 10 elif a < 10: length = 1 else: length = 100elif a < 10000: length = 1000elif a < 100000: length = 10000else: print('Error')while length: print(a // length) a %= length length //= 10
c = int(input("Please enter a number: "))w = 10000length = 5flag = Falsewhile w: t = c // w if flag: print(t) else: if t: print(t) flag = True else: length -= 1 c = c % w w //= 10print("The length of number is", length)
a = int(input("Please enter a number: "))for i in range(a): if i == 0 or i == a-1: print('*' * a ) else: print('*' + ' ' * (a - 2) + '*')
a = int(input("Please enter a number: "))print('*' * a)for i in range(a - 2): print('*' + ' ' * (a - 2) + '*')else: print('*' * a)
a = int(input("Please enter a number: "))top = mid = '*'for i in range(a - 1): top += '\t*' mid += '\t'else: mid += '*' print(top)for i in range(a - 2): print('\n') print(mid)else: print('\n') print(top)
sum = 0for i in range(1,100,2): sum += iprint(sum)
a = int(input('Please enter a score: '))if a > 79: if a >= 90: print('A') else: print('B')elif a >= 70: print('C')elif 60 <= a < 69: print('D')else: print('E')
sum = 0for i in range(1,6): mul = 1 for j in range(1,i + 1): mul *= j sum += mulprint(sum)
sum = 0mul = 1for i in range(1,6): mul *= i sum += mulprint(sum)
a=int(input("Please enter a number: "))for i in range(2,int(a ** 0.5) + 1): if a % i != 0: continue elif a % i == 0: print(("N: %d") % (i)) break else: print("Yes,prime")
for i in range(1,10): line = '' for j in range(1,i + 1): line = line + ('%d*%d=%2d ') % (j,i,i*j) print(line)
a = int(input(">>>"))b = (a - 1) / 2for i in range(a): if i <= b: print(' ' * ((a - (2 * i + 1)) // 2) +'*' * (i * 2 + 1) + ' ' * (( a - (2 * i + 1)) // 2 )) else: m=int(2 * b - i) print(' '*((a - (2 * m + 1)) // 2) +'*' * (m * 2 + 1) +' ' * ((a - (2 * m + 1)) // 2))
a = int(input(">>>"))b=(a - 1) // 2for i in range(-b,b + 1): c = abs(i) print(' ' * c + '*' * (a - 2 * c) + ' ' * c)
a = 1b = 1while True: ne = a + b if ne >= 100: break a = b b = ne print(ne)
a = 1b = 1cnt = 2while True: ne = a + b a = b b = ne cnt += 1 if cnt == 101: print(b) break
import datetimestart=datetime.datetime.now()cnt=1for i in range(3,100000,2):#优化点1:排除偶数 for j in range(3,int(i ** 0.5) + 1,2):#优化点2:排除含有因子2的数并只循环到原数的开方加1 if i % j == 0: break else: #print(i) cnt += 1end=(datetime.datetime.now() - start).total_seconds() print(end)print(cnt)
a = int(input('>>>'))import datetimestart=datetime.datetime.now()cnt = 2for i in range(4,a): if i % 6 != 1 and i % 6 != 5:#质数必与6的倍数相邻 continue else: for j in range(5,int(i ** 0.5) + 1,2): if i % j == 0: break else: cnt += 1end=(datetime.datetime.now()-start).total_seconds() print(end) print(cnt)
x = 5step = 2count = 2while x < 100000: for i in range(3,int(x ** 0.5) + 1,2): if not x % i: break else: count += 1 x += step step = 4 if step == 2 else 2 print(count)
prime=[2]for i in range(3,100000,2): flag=False up=int(i**0.5)+1 for j in prime: if i%j==0: flag=False break if j>=up: flag=True break if flag: prime.append(i)print(len(prime))
筛选法待补充
temp = 0 while True: a = input('>>>') if not a: temp = a break else: b = int(a) if b >= temp: temp = bprint(temp)
sum = 0cnt = 0while True: a = input('>>>') if not a: break else: sum += float(a) cnt += 1 print(sum / cnt)
for i in range(1,10): line = '' for j in range(i,10): line += '%d*%d=%2d ' % (i,j,j * i) print(' ' * (7 * i - 6) + line)
for i in range(1,10): line = '' for j in range(i,10): line += '%d*%d=%2d ' % (i,j,j * i) print('{:>63}'.format(line))
for i in range(1,10): line = '' for j in range(i,10): line += '{}*{}={:<{}} '.format(i,j,j * i,2 if j<4 else 3) print('{:>70}'.format(line))
a = int(input(">>>"))b = (a - 1) // 2for i in range(-b,b + 1): c = int((a - (2 * abs(i) + 1)) / 2) print(' ' * c + '*' * (2 * abs(i) + 1) + ' ' * c)
num = int(input(">>>"))if num % 2 == 0: num += 1half = (num - 1) // 2for i in range(- half,half + 1): if i < 0: print(' ' * (-i) + '*' * (half + i + 1)) elif i == 0: print('*' * num) else: print(' ' * half + '*' * (half - i + 1))
x = 1for i in range(9): x = (x + 1) * 2print(x)
转载于:https://blog.51cto.com/11281400/2090633