以上三角形的数阵,第一行只有一个数1,以下每行的每个数,是恰好是它上面的数、左上角数和右上角的数,3个数之和(如果不存在某个数,认为该数就是0)。
求第n行第一个偶数出现的位置。如果没有偶数,则输出-1。例如输入3,则输出2,输入4则输出3,输入2则输出-1。
数据范围:
while True: try: n = int(input()) if n ==1&nbs***bsp;n ==2: print(-1) elif n %2 != 0: # 经过观察,所有行的第二个数和倒数第二个数为上一行对应位置加1递增,则奇数行的第一个偶数就是第二个数 print(2) else: # n=4,6,8,10... 经过观察,偶数行n除去头尾两个1之外共需计算2个由2数相加得到的2-th数,2n-1-4个由3个数相加得到的数 preRow = [1,2,3,2,1] # 第三行起始数据 YangTriangle = [] for i in range(n-3): secondNum = preRow[0] + preRow[1] if secondNum %2 ==0 and i==n: print(2) else: row = [1] * (2*(4+i) - 1) # 第i行需要计算3+i个3个相加的数 for j in range(2*(4 + i) - 5): row[j+2] = preRow[j] + preRow[j+1] + preRow[j+2] row[1],row[-2] = secondNum,secondNum YangTriangle.append(row) # update preRow preRow = row # find first even number in the n-th row for ind, x in enumerate(row): if x %2 ==0: print(ind+1) break except: break
import sys while True: try: lines = int(sys.stdin.readline()) list = [] list.append([1,]) list.append([1, 1, 1]) for line in range(2, lines): i = 1 + 2 * line list.append([0] * i) for j in range(i): if j == 0: list[line][j] = list[line - 1][0] elif j == 1: list[line][j] = list[line - 1][0] + list[line - 1][1] elif 1 < j < i - 2: list[line][j] = list[line - 1][j-2] + list[line - 1][j-1] + list[line - 1][j] elif j == i - 2: list[line][j] = list[line - 1][i-3] + list[line - 1][i-4] elif j == i - 1: list[line][j] = list[line - 1][i-3] # print(list) for i in range(len(list[lines - 1])): if list[lines - 1][i] % 2 == 0: print(i + 1) break except: break分析上一行与下一行的关系,将每一行的数值存起来。然后遍历最后一行,即可。
import sys def yanghui(now, line = [1], n = 1): if n == now: return line else: n += 1 nextLine = [1, 1] if n == 2: nextLine = [1, 1, 1] elif n > 2: nextLine = [1, n-1, n-1, 1] for i in range(2, 2 * n - 3): nextLine.insert(i, sum(line[i - 2:i + 1])) return yanghui(now, nextLine, n) for i in sys.stdin: i = int(i.strip()) line = yanghui(i) notfind = True for index in range(i): if line[index]%2 == 0: print(index + 1) notfind = False break if notfind: print(-1)
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = new Integer(scanner.nextLine()); List<Integer[]> numList = new ArrayList<>(); //知道N就知道最多一行的数字的数量为2n-1了 所以直接让每一行都是2n-1个数 //初始化第一行 最中间那个数为1 其余为0 Integer[] nums0 = new Integer[2 * n - 1]; Arrays.fill(nums0, 0); numList.add(nums0); nums0[n - 1] = 1; boolean flag = true; //从第二行开始遍历 for (int i = 1; i < n; i++) { //每一行都是2n-1行 Integer[] nums = new Integer[2 * n - 1]; Arrays.fill(nums, 0); //取出上一行 Integer[] pre = numList.get(i - 1); //因为是对称的 所以只用遍历到第n-1个数 for (int j = 0; j < n; j++) { //首尾两个数需要 特殊处理一下 if(j==0){ nums[j] = pre[j] + pre[j + 1]; nums[2 * n - 2 - j] = pre[2 * n - 3 - j] + pre[2 * n - 2 - j]; } //其余每一位都是上一层的三个数求和 else { nums[j] = pre[j - 1] + pre[j] + pre[j + 1]; //第N行 if (i == n - 1) { //当出现偶数时 打印并退出循环 if ((nums[j] & 1) == 0) { System.out.println(j + 1); flag = false; break; } } //放这里纯为了少算一次 nums[2 * n - 2 - j] = pre[2 * n - 3 - j] + pre[2 * n - 2 - j] + pre[2 * n - 1 - j]; } } numList.add(nums); } if (flag) System.out.println(-1); } } }
import java.util.Scanner; //杨辉三角规律 行号 第一个偶数在该行第几个 // 1 1 -1 // 1 1 1 2 -1 // 1 2 3 2 1 3 2 // 1 3 6 7 6 3 1 4 3 // 1 4 10 16 19 16 10 4 1 5 2 // 1 5 15 30 45 51 45 30 15 5 1 6 4 // // 首个偶数在该行第几个的规律: -1 -1 (2 3 2 4)···(2 3 2 4) public class MainHJ53 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] res = new int[]{2,3,2,4}; while(sc.hasNext()){ int n = sc.nextInt(); System.out.println(res[(n+1)%4]); } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); Main a = new Main(); while (scanner.hasNext()){ int row = Integer.parseInt(scanner.nextLine()); System.out.println(a.firstEven(row)); } } public int firstEven(int row){ if ((row == 1 || row == 2)){ return -1; } int result = row % 2 != 0 ? 2 : (row % 4 == 0 ? 3 : 4); return result; } }
#include <iostream> #include <vector> using namespace std; int main(){ int n; while(cin >> n){ vector<vector<int>> vv(n); for(int i = 0; i < n; ++i){ vv[i].resize(i * 2 + 1, 1); } for(int i = 2; i < n; ++i){ for(int j = 1; j < i * 2; ++j){ if(j == 1){ vv[i][j] = vv[i - 1][j - 1] + vv[i - 1][j]; } else if(j == i * 2 - 1){ vv[i][j] = vv[i - 1][j - 2] + vv[i - 1][j - 1]; } else{ vv[i][j] = vv[i - 1][j - 2] + vv[i - 1][j - 1] + vv[i - 1][j]; } } } int i = 0; for(; i < (n - 1) * 2 + 1; ++i){ if(vv[n - 1][i] % 2== 0){ cout << i + 1 << endl; break; } } if(i == (n - 1) * 2 + 1){ cout << -1 << endl; } } return 0; }
#include <stdio.h> (737)#include <stdlib.h> // 奇数+奇数=偶数 奇数+偶数=奇数 // 偶数+偶数=偶数 // 奇+奇+奇 = 奇 // 偶+偶+偶 = 奇 // 这种方法也可以得出当前值 #define N (1000) // 不符合题意n <= 1000000000,但是通过了所有示例 int dp[N][N + 4] = { 0 }; int main(void) { int n = 0; while (scanf("%d",&n)!=EOF) { //int dp[n+3][n+3]; for (int i = 0; i <= n; i++) { dp[i][0] = 0; //偶数 dp[i][1] = 1; //奇数 } for (int i = 1; i <= n; i++) { for (int j = 2; j <= i+2; j++) { dp[i][j] = dp[i - 1][j - 2] + dp[i - 1][j - 1] + dp[i - 1][j]; if (dp[i][j] == 2) dp[i][j] = 0; else if (dp[i][j] == 3) dp[i][j] = 1; } } for (int i = 1; i <= n; i++) { if (dp[n-1][i] == 0) { printf("%d\n", i); break; } } } return 0; }
//找规律的过程稍微麻烦了一点,注意边界条件,然后n-1那些条件再做的时候可能还是会看不懂 //大意提示:存储一半的表,然后根据计算公式由上一行得到下一行的值 #include<iostream> using namespace std; int main(){ int n; while(cin>>n){ if(n==1||n==2){ cout<<"-1"<<endl; } else{ int** a=new int*[n]; for(int i=0;i<n;i++) a[i]=new int[n]; for(int i=0;i<n-1;i++) a[0][i]=0; a[0][n-1]=1; for(int i=1;i<n;i++){ for(int j=0;j<n-i-1;j++) a[i][j]=0; a[i][n-i-1]=1; for(int j=n-i;j<n-1;j++) a[i][j]=a[i-1][j-1]+a[i-1][j]+a[i-1][j+1]; a[i][n-1]=2*a[i-1][n-2]+a[i-1][n-1]; } bool judge=1; for(int i=0;i<n;i++) if(a[n-1][i]%2==0){ cout<<i+1<<endl; judge=0; break; } if(judge) cout<<"-1"<<endl; } } }
/*利用矩阵生成杨辉三角矩阵的变形,然后在最后一行查找*/ def getIndex(n): matrix = [[0 for j in range(2*n - 1)] for i in range(n)] matrix[0][n-1] = 1 for i in range(1,n): for j in range(1, 2*n-2): matrix[i][j] = matrix[i-1][j-1] + matrix[i-1][j] + matrix[i-1][j+1] matrix[n-1][0] = 1 matrix[n-1][2*n-2] = 1 for k in range(2*n-2): if matrix[n-1][k] % 2 == 0: return k+1 return -1 while True: try: n = int(input()) print(getIndex(n)) except: break
#include<iostream> #include<vector> using namespace std; int f(int a,int b) { if(a<1||b<1||b>a) return 0; else if(b==1) return 1; else if(a!=b) { return f(a-1,b-1)+f(a-1,b-2)+f(a-1,b); } else if(a==b) return f(a-1,b-1)+2*f(a-1,b-2); else cout<<"err"; return 0; } int main() { int a; while (cin >> a) { int flag = 0; for (int i = 1; i <= a; i++) { if (!(f(a, i) & 1)) { cout << i << endl; flag = 1; break; } } if (flag ==0) cout << -1 << endl; } return 0; }
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cstring> #include <string> #include <cmath> #include <cctype> using namespace std; int main() { int n; while(cin>>n) { if(n<=2) cout<<-1<<endl; else if(n%2==1) cout<<2<<endl; else if(n%4==0) cout<<3<<endl; else cout<<4<<endl; } return 0; }
1.输入n,根据n生成一个n *(2n-1)的矩阵arr[n][2*n-1]; 2.其中arr[0][n-1]=1,arr[i][j]=arr[i-1][j-1]+arr[i-1][j]+arr[i-1][j+1],超出边界的取0; 3.由于对称性,只需要判断第n行的中间那个元素即可,即到arr[n-1][i](1<=i<=n-1); #include<iostream> #include<algorithm> using namespace std; vector<vector<int> > fun(int n) { vector<vector<int> > ans(n, vector<int>(2*n-1,0)); ans[0][n-1]=1; for(int i=1;i<n-1;i++) { for(int j=n-1-i;j<=n-1+i;j++) ans[i][j]=ans[i-1][j-1]+ans[i-1][j]+ans[i-1][j+1]; } ans[n-1][0]=1; ans[n-1][2*n-2]=1; for(int j=1;j<2*n-2;j++) ans[n-1][j]=ans[n-2][j-1]+ans[n-2][j]+ans[n-2][j+1]; return ans; } int main() { int n; while(cin>>n) { if(n<=0) return -1; vector<vector<int> >ans=fun(n); int i=0; bool flag=false; for(i=1;i<=n-1;i++) { if( (ans[n-1][i]) % 2 == 0) { cout<<(i+1)<<endl; flag=true; break; } } if( flag==false ) cout<<"-1"<<endl; } return 0; }
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNextInt()){ int num = sc.nextInt(); calculate(num); } sc.close(); } public static void calculate(int num){ int[] arr = {1}; for(int i=0;i<num-1;i++){ arr = getNextRow(arr); } for(int i=0;i<arr.length;i++){ if(arr[i]%2==0){ System.out.println(i+1); break; } } } public static int[] getNextRow(int[] row_num){ if(row_num.length==1){ int[] row_2 = {1,1,1}; return row_2; } else{ int[] row_next = new int[row_num.length+2]; row_next[0]=1; row_next[row_next.length-1]=1; row_next[1]=1+row_num[1]; row_next[row_next.length-2]=1+row_num[row_num.length-2]; for(int i=2;i<row_next.length-2;i++){ row_next[i]=row_num[i-1]+row_num[i]+row_num[i-2]; } return row_next; } } }