目录
1.年龄巧合 694
def check(x):
x=str(x)
a=int(x[0])+int(x[1])+int(x[2])+int(x[3])
if a==2014-int(x):
return True
else:
return False
y=[]
for i in range(2014,1980,-1):
if check(i):
print(i)
2.第几个幸运数字 613
1.暴力
count=0
for i in range(30):
for j in range(30):
for k in range(30):
if 3**i*5**j*7**k<=59084709587505:
count+=1
print(count-1)
2.优先队列
from queue import *
num=[3,5,7]
vis=set()
q=PriorityQueue()
for i in num:
q.put(i)
res=0
while not q.empty():
next_item=q.get()
res+=1
for every in num:
now=every*next_item
if now not in vis:
q.put(now)
vis.add(now)
if next_item==59084709587505:
print(res)
break
3.奇数倍数 818
def check(x):
res=0
x=str(x)
for i in x:
if int(i)%2==1:
res+=1
if res==len(x):
return True
for i in range(3,100):
a=i*2019
if check(a):
print(a)
4.迷宫 602
可以手算
1.用到bfs
2.用队列记录坐标,步数,方向
3.每记录一个坐标就打一个标记(越界,有障碍,有标记)continue
4.按照字典序从小到大的顺序走,下左右上的顺序,只要没有障碍和标记
import queue
Map = """01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000"""
Map = Map.split('\n')
n = 30
m = 50
vis = [[0 for i in range(m)] for j in range(n)]
dir = [[1, 0], [0, -1], [0, 1], [-1, 0]]
dir_s = "DLRU"
q = queue.Queue()
def bfs():
q.put((0, 0, 0, "")) #(x, y, step, path)
vis[0][0] = 1
while q.empty() == False:
now = q.get()
if now[0] == n - 1 and now[1] == m - 1:
print(now[3])
break
for i in range(4):
next_x = now[0] + dir[i][0]
next_y = now[1] + dir[i][1]
if next_x < 0 or next_x >= n or next_y < 0 or next_y >= m:
continue
if vis[next_x][next_y] == 1:
continue
if Map[next_x][next_y] == '1':
continue
next_step = now[2] + 1
next_path = now[3] + dir_s[i]
q.put((next_x, next_y, next_step, next_path))
vis[next_x][next_y] = 1
bfs()
5.纸牌三角形 639
from itertools import *
ans=0
for i in permutations(range(1,10)):
a=i[0]+i[1]+i[2]+i[3]
b=i[3]+i[4]+i[5]+i[6]
c=i[6]+i[7]+i[8]+i[0]
if a==b==c:
ans+=1
print(int(ans/6))
6.取球游戏 278
dp = [0 for i in range(10001)]
dp[2] = dp[4] = dp[6] = dp[8] = 1
for i in range(9,len(dp)):
if dp[i - 1] == 0:
dp[i] = 1
elif dp [i - 3] == 0:
dp[i] = 1
elif dp[i-7] == 0:
dp[i] = 1
elif dp[i-8] == 0:
dp[i] = 1
n = int(input())
for i in range(n):
print(dp[int(input())])