描述 | |
---|---|
知识点 | 字符串 |
运行时间限制 | 10M |
内存限制 | 128 |
输入 | 输入一个int整数 |
输出 | 输出返回的int值 |
样例输入 | 4 |
样例输出 | 3 |
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int lineNum = scanner.nextInt();
scanner.close();
System.out.println(localOfEvenNum(lineNum));
System.out.println(method_2(lineNum));
}
private static int localOfEvenNum(int num) {
int triangle[][] = new int[num][num * 2]; // 创建二维数组
for (int i = 0; i < triangle.length; i++) { // 遍历二维数组的第一层
for (int j = 0; j <= i * 2; j++) { // 遍历第二层数组
if (i == 0 || j == 0 || j == i * 2 || (i == 1 && j == 1)) {
triangle[i][j] = 1; // 将两侧的数组元素赋值为1
} else if (j == 1 && i != 1) { // 其他数值通过公式计算
triangle[i][j] = triangle[i - 1][j]
+ triangle[i - 1][j - 1];
} else {
triangle[i][j] = triangle[i - 1][j]
+ triangle[i - 1][j - 1] + triangle[i - 1][j - 2];
}
}
}
int local = 0;
for (int i = 0; i < num * 2; i++) {
if (triangle[num - 1][i] % 2 == 0) {
local = i + 1;
break;
}
}
if (num < 3)
local = -1;
return local;
}
private static int method_2(int n) { //通过规律得到答案
if (n < 3)
return -1;
if (n % 2 == 1)
return 2;
else
return n / 2 % 2 + 3;
}
}