输入一个整数
代表询问的行数。
输出一个整数,代表第
行中第一个偶数出现的位置。特别地如果第
行中没有偶数,则输出
。
4
3
3
2
1
-1
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; } } }
#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; }
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; } } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int num = sc.nextInt(); System.out.println(res(num)); } public static int res(int num){ if(num == 1 || num == 2){ return -1; } num -= 2; if(num%2 == 1){ return 2; } if(num%2 == 0 && num%4 != 0){ return 3; } if(num%2 == 0 && num%4 == 0){ return 4; } return 0; } }找规律
import java.util.Scanner; /** * @author Yuliang.Lee * @version 1.0 * @date 2021/9/22 17:59 * 杨辉三角的变形: 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 以上三角形的数阵,第一行只有一个数1,以下每行的每个数,是恰好是它上面的数,左上角数到右上角的数,3个数之和(如果不存在某个数,认为该数就是0)。 求第n行第一个偶数出现的位置。如果没有偶数,则输出-1。例如输入3,则输出2,输入4则输出3。 * 示例: 输入: 4 2 输出: 3 -1 */ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int rowNum = in.nextInt(); // 定义二维数组 int colNum = 2 * rowNum - 1; int[][] arr = new int[rowNum][colNum]; // 初始化首行 for (int i = 0; i < colNum; i++) { arr[0][i] = 0; if (i == colNum / 2) { arr[0][i] = 1; } } // 填充数组 for (int i = 1; i < rowNum; i++) { for (int j = 0; j < colNum; j++) { if (j == 0) { arr[i][j] = arr[i-1][j] + arr[i-1][j+1]; } else if (j == colNum - 1) { arr[i][j] = arr[i-1][j-1] + arr[i-1][j]; } else { arr[i][j] = arr[i-1][j-1] + arr[i-1][j] + arr[i-1][j+1]; } } } // 找最后一行的数字中第一次出现偶数的位置 int result = -1; for (int j = 0; j < colNum; j++) { if (arr[rowNum - 1][j] != 0 && arr[rowNum - 1][j] % 2 == 0) { result = j + 1; break; } } System.out.println(result); } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ int n = in.nextInt(); if(n<=2) System.out.println(-1); else if(n%2==1){ System.out.println(2); }else{ if(n%4==0) System.out.println(3); else System.out.println(4); } } in.close(); } }
记录了自己的ac代码,欢迎pull request
https://github.com/SpecialYy/HUAWEI_Programming.git
package com.special.spet;
import java.util.Scanner;
/**
* 杨辉三角变形
* 注意每一行的规律,每一行有2 * i - 1个数,中间的数位于第i个位置
* 从第1行开始(而不采用第0行)方便后面的计算
* map[i][j] = map[i - 1][j - 1] + map[i - 1][j - 2] + map[i - 1][j]
* 注意若采用上式,在算map[i][1]直接赋值,否则map[i - 1][j - 2]会数组越界
* 判断第几位是偶数时,只需循环到中间位置即可,因为每一行的数是对称的。
* @author special
* @date 2017年11月22日 下午11:15:13
*/
public class Pro52 {
public static int getFirstPlaceEven(int n){
int[][] map = new int[n + 1][2*n];
map[1][1] = 1;
for(int i = 2; i <= n; i++){
map[i][1] = 1;
for(int j = 2; j <= 2 * i - 1; j++)
map[i][j] = map[i - 1][j - 1] + map[i - 1][j - 2] + map[i - 1][j];
}
for(int i = 1; i <= n; i++)
if(map[n][i] % 2 == 0)
return i;
return -1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
while(input.hasNext()){
int n = input.nextInt();
int firstEven = getFirstPlaceEven(n);
System.out.println(firstEven);
}
}
}