以上三角形的数阵,第一行只有一个数1,以下每行的每个数,是恰好是它上面的数、左上角数和右上角的数,3个数之和(如果不存在某个数,认为该数就是0)。
求第n行第一个偶数出现的位置。如果没有偶数,则输出-1。例如输入3,则输出2,输入4则输出3,输入2则输出-1。
数据范围:
while True: try: a = int(input()) l_0 = [[0 for i in range(2 * a - 1)] for i in range(a)] l_0[0][0] = 1 for i in range(1,a): for j in range(len(l_0[0])): l_0[i][j] = l_0[i-1][j-2] + l_0[i-1][j-1] + l_0[i-1][j] for k in l_0[a-1]: if k % 2 == 0: print(l_0[a-1].index(k) + 1) break else: print('-1') except: break暴力破解
import sys # 计算第n列的数字,返回其数组 def func(n): # 初始化 arr = [0 for i in range(2*n-1)] arr[n-1] = 1 for i in range(n-1): # 临时存储,用于保存第j次的每项结果 tmp_arr = [0]*(2*n-1) for j in range(2*n-1): # 第一个元素 if j == 0: tmp_arr[j] = arr[j] + arr[j+1] # 最后一个元素 elif j == 2*n-2: tmp_arr[j] = arr[j-1] + arr[j] # 中间元素 else: tmp_arr[j] = arr[j-1] + arr[j] + arr[j+1] # 将第i次遍历的数据存入数组 arr = tmp_arr return arr # 找到第一个偶数 def func2(arr): for i in range(len(arr)): if arr[i] % 2 == 0: return i+1 return -1 for line in sys.stdin: num = int(line.strip()) print(func2(func(num)))
def num(n): if n == 1: return 1 else: return num(n-1) + 2 def tria(L,cen): n = 3 while n<=cen: number = num(n) for i in range(1,number-2): if i == 1: L.append([]) L[-1].append(1) L[-1].append(n-1) L[-1].append(n-1) L[-1].append(1) else: L[-1].insert(i,L[-2][i]+L[-2][i-1]+L[-2][i-2]) n = n + 1 while True: L = [[1], [1, 1, 1]] list_1 = [] try: cen = int(input()) tria(L,cen) for j in L[cen-1]: list_1.append(j%2) if 0 not in list_1: print(-1) else: print(list_1.index(0) + 1) except: break
while True: try: n = int(input()) if n <= 2: print(-1) else: prev = [] curr = [1, 1, 1] for i in range(2, n): prev = curr temp = [] for j in range(len(prev)-2): temp.append((prev[j]+prev[j+1]+prev[j+2])%2) curr = [1] + [(prev[0]+prev[1])%2] + temp + [(prev[-2]+prev[-1])%2] + [1] flag = 1 for i in range(len(curr)): if curr[i] == 0: print(i+1) flag = 0 break if flag == 1: print(-1) except: break
''' 1 1 1 1 1 2 3 2 1 1 3 6 7 6 3 1 1 4 10 16 19 16 10 4 1 数阵特点: 每一行每个数都是上面三个数之和,例如第4行第4个数为7, 上面三个数分别为2,3,2,其和刚好为7 问:第n行第1个偶数出现的位置,不存在偶数,则输出为-1 简要分析: 因只考察奇、偶,可用1代表奇,0代表偶,又因数阵为对称图形, 只观察左半第一个为0的位置即可 1 1 1 1 0 1 开头为1010(第2位出现偶数) 1 1 0 1 开头为1101(第3位出现偶数) 1 0 0 0 1 开头为1000(第2位出现偶数) 1 1 1 0 1 1 开头为1110(第4位出现偶数) 1 0 1 0 0 0 1 开头为1010(重复) 1 1 0 1 1 0 1 1 开头为1101(重复) 第3到n行输出为"2324"....,以4为周期的重复 ''' # 解法1:找规律法(运行时间14ms) # while True: # try: # n = int(input()) # if n <= 2: # print(-1) # else: # remain = (n-2) % 4 # if remain == 1: # print(2) # elif remain == 2: # print(3) # elif remain == 3: # print(2) # elif remain == 0: # print(4) # except: # break # 解法2:直接查询法(运行时间112ms) # 获取数阵第n行,第i个数,1<=i<=2n-1 def get_num(n, i): if i > n: i = 2*n-i if n <= 2: num = 1 else: if i == 1: num = 1 elif i == 2: num = get_num(n-1, 2) + get_num(n-1, 1) else: num = get_num(n-1, i-1) + get_num(n-1, i-2) + get_num(n-1, i) return num # 查看前10行数阵 # for n in range(1, 10): # for i in range(1,2*n): # print(get_num(n,i), end=" ") # print("") while True: try: n = int(input()) # 前两行不存在偶数 if n <= 2: print(-1) else: for i in range(1,2*n): # 依次判断当前值是否为偶数 if get_num(n, i) % 2 ==0: print(i) break except: break
import sys for line in sys.stdin: n = int(line) # 定义二维数组 # N = [[0]*10 for i in range(10)] yanghui_triaang = [[0]*(2*n-1) for i in range(n)] yanghui_triaang[0][0] = 1 yanghui_triaang[1][0], yanghui_triaang[1][1], yanghui_triaang[1][2] = 1, 1, 1 # print(yanghui_triaang) if n == 0&nbs***bsp;n == 1: print(-1) else: for i in range(2, n): # 将杨辉三角形劈成一半,前半与后半顺序正好相反 for j in range(n): if j == 0: yanghui_triaang[i][j] = 1 elif j == 1: yanghui_triaang[i][j] = 1 + yanghui_triaang[i-1][j] else: yanghui_triaang[i][j] = yanghui_triaang[i-1][j-2]+yanghui_triaang[i-1][j-1]+yanghui_triaang[i-1][j] for i in range(2, n): for j in range(i, 2*i+1): yanghui_triaang[i][j] = yanghui_triaang[i][j - (j-i)*2] # print(yanghui_triaang) # 输入n # 输出第N行第一个偶数的位置,若没有则输出-1 flag = False for ele in yanghui_triaang[n-1]: if ele % 2 == 0: flag = True print(yanghui_triaang[n-1].index(ele)+1) break if flag == False: print(-1)
# python 3 递归 # case 通过率60% def calculate(i,j): if i==1: return 1 elif j<=0&nbs***bsp;j>2*i+1: return 0 else: return calculate(i-1,j-2)+calculate(i-1,j-1)+calculate(i-1,j) while True: try: n=int(input()) if n==1: print(-1) for i in range(1,n+1): if calculate(n,i)%2==0: print(i) flag=1 break if flag!=1: print(-1) except: break
## python 3 动态规划 def dP(n): li=[[0 for j in range(n+1)] for i in range(n+1)] for i in range(1,n+1): # 因为不能使用numpy模块,只能使用for循环赋值。 li[i][1]=1 for i in range(1,n+1): for j in range(2,n): li[i][j]=li[i-1][j-2]+li[i-1][j-1]+li[i-1][j] #print(li) for i in range(1,n): if li[n][i]%2==0: flag=1 break #print(i) return i if flag==1 else -1 while True: try: n=int(input()) if n==1: print(-1) print(dP(n)) except: break