一 遍历杨辉三角左半部分(此方法运行到10000时提示越界,通过率73%)
import java.util.*;
public class YangHui {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] arr = new int[n][];
for(int i = 0; i < arr.length; i++){
arr[i] = new int[i+1];
arr[i][0] = 1;
for (int j = 1; j < arr[i].length; j++) {
if(i <= 1){
arr[i][j] = 1;
} else {
if(j == 1){
arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
}
else if(j == arr[i].length - 1){
arr[i][j] = (arr[i-1][j-2] * 2) + arr[i-1][j-1];
}
else{
arr[i][j] = arr[i-1][j-2] + arr[i-1][j-1] + arr[i-1][j];
}
}
}
}
int index = -1;
for (int j = 0; j < n ; j++) {
if (arr[n - 1][j] % 2 == 0){
index = j + 1;
break;
}
}
System.out.println(index);
}
}
二 找规律(通过100%)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
/*
1- 0 1
2- 0 1 1
3- 2 1 2 3
4- 3 1 3 6 7
5- 2 1 4 10 16 19
6- 4 1 5 15 30 45 51
7- 2 1 6 21 50 90 126 141
8- 3 1 7 28 77 161 266 357 393
9- 2 1 8 36 112 266 504 784 1016 1107
10- 4 1 9 45 156 414 882 1554 2304 2907 3139
规律:
刨去前两行,每4行一个轮回,位数2,3,2,4循环;
*/
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int index = -1;
if(n > 2){
if((n-2) % 4 == 1 || (n-2) % 4 == 3){
index = 2;
}
else if((n-2) % 4 == 2){
index = 3;
}
else if((n-2) % 4 == 0){
index = 4;
}
}
System.out.println(index);
}
}