Problem solving 0. Print less than 10 even: bit operation
for i in range(10): if not i & 0x01: print(i)
1. Given a positive integer that does not exceed 5 bits, it is judged by a few (using the Input function) method one: normal logical processing
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‘)
Method Two: Binary
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‘)
Method Three: String processing
a = str(int(input("Please enter a number: ")))print(len(a))
2. Given a positive integer no more than 5 bits, judge the number of digits, then print out the number of bits, 10 bits, hundred, thousand, million digit method one:
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
Method Two:
a = input("Please enter a number: ")b = int(a)print(len(a))for i in range(len(a)): print(b % 10) b = b // 10
Method Three:
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
Method Four:
a=int(input("Please enter a number: "))a *= 10while (a // 10) != 0: a //= 10 print(a % 10)
3. Given a positive integer no more than 5 bits, judge the number of digits, then print out the million, thousand, hundred, 10 bits, digits of the number method one:
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))
Method Two:
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
Method Three:
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
Method Four:
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)
4. Print a square method with an edge length of n one:
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) + ‘*‘)
Method Two:
a = int(input("Please enter a number: "))print(‘*‘ * a)for i in range(a - 2): print(‘*‘ + ‘ ‘ * (a - 2) + ‘*‘)else: print(‘*‘ * a)
Method Three:
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)
5. Ask for all odd and (2500) method one in 100:
sum = 0for i in range(1,100,2): sum += iprint(sum)
6. Judge student achievement, Grade a~e. Among them, 90 points above is ' A ', 80~89 is divided into ' B ', 70~79 is divided into ' C ', 60~69 is divided into ' D ', and 60 points below is ' E ' method one:
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‘)
7. Ask for 1 to 5 factorial sum method one:
sum = 0for i in range(1,6): mul = 1 for j in range(1,i + 1): mul *= j sum += mulprint(sum)
Method Two: High efficiency
sum = 0mul = 1for i in range(1,6): mul *= i sum += mulprint(sum)
8. Give a number to determine whether it is prime (prime)
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")
9. Print 99 multiplication table
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)
10. Print Diamond Method One:
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))
Method Two:
a = int(input(">>>"))b=(a - 1) // 2for i in range(-b,b + 1): c = abs(i) print(‘ ‘ * c + ‘*‘ * (a - 2 * c) + ‘ ‘ * c)
11. Print the Fibonacci sequence within 100
a = 1b = 1while True: ne = a + b if ne >= 100: break a = b b = ne print(ne)
12.101th of the Fibonacci sequence 573147844013817084101
a = 1b = 1cnt = 2while True: ne = a + b a = b b = ne cnt += 1 if cnt == 101: print(b) break
13. For all primes within 100,000 9592 method one:
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)
Method Two:
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)
Method Three:
筛选法<br/>待补充<br/>
14. Enter a number of integers to print the maximum value:
temp = 0 while True: a = input(‘>>>‘) if not a: temp = a break else: b = int(a) if b >= temp: temp = bprint(temp)
15. Enter the number of N to calculate the average of the arithmetic after each input
sum = 0cnt = 0while True: a = input(‘>>>‘) if not a: break else: sum += float(a) cnt += 1 print(sum / cnt)
16. Reverse Multiplication Table: Method One:
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)
Method Two:
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))
Method Three:
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))
17. Print to the top triangle:
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)
18. Print Lightning:
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))
19. Monkeys Eat Peach:
x = 1for i in range(9): x = (x + 1) * 2print(x)
Python First week study notes (2)