题目
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 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
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] res = new int[n+1][2*n];
for(int i=1;i<=n;i++) {
res[i][1] = 1;
res[i][2*i-1] = 1;
}
//第二列
for(int i=2;i<=n;i++) {
res[i][2] = res[i-1][1]+res[i-1][2];
}
//大于等于第三列
for(int i=3;i<=n;i++) {
for(int j=3;j<=(2*n-1);j++) {
res[i][j] = res[i-1][j-2]+res[i-1][j-1]+res[i-1][j];
}
}
for(int j=1;j<=(2*n-1);j++) {
if(res[n][j]%2 == 0) {
System.out.println(j);
return ;
}
}
System.out.println(-1);
sc.close();
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 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
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] res = new int[n][2 * (n - 1) + 1];
res[0][0] = 1;
for (int i = 1; i < n; i++) {
res[i][0] = 1;
res[i][2 * i] = 1;
}
//第二列
for (int i = 1; i < n; i++) {
res[i][1] = res[i - 1][0] + res[i - 1][1];
}
//大于等于第三列
for (int i = 2; i < n; i++) {
for (int j = 2; j < 2 * (n - 1) + 1; j++) {
res[i][j] = res[i - 1][j - 2] + res[i - 1][j - 1] + res[i - 1][j];
}
}
for (int j = 0; j < 2 * (n - 1) + 1; j++) {
if (res[n - 1][j] % 2 == 0) {
System.out.println(j + 1);
return ;
}
}
System.out.println(-1);
// for(int i=0;i<n;i++) {
// for(int j=0;j<(2*(n-1)+1);j++){
// System.out.print(res[i][j]);
// }
// System.out.println();
// }
sc.close();
}
}
下标为0或者1都不能通过全部样例
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
if(num == 1 || num == 2){
System.out.println(-1);
}
else if(num % 4 == 1 || num % 4 == 3){
System.out.println(2);
}
else if(num % 4 == 0){
System.out.println(3);
}
else if(num % 4 == 2){
System.out.println(4);
}
}
}