题解 | #杨辉三角的变形#
杨辉三角的变形
https://www.nowcoder.com/practice/8ef655edf42d4e08b44be4d777edbf43
用实在的数组去一行行计算,最后总是有些超大的输入导致运行超时,所以部分用例过不了。
后来发现,找数字规律就行了:。。。。(侮辱性极强😅😅)
import java.util.Scanner;
//如果n为奇数,一定是第二个数字为偶数,因为第二个数字恒为n-1;如果n为偶数,当n为(4,6,8,10.....)时分别是(3,4,3,4....)
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int a = in.nextInt();
int out = 0;
if (a > 2 && a % 2 == 1) {
out = 2;
} else if (a > 2 && a % 2 == 0) {
if ((a / 2) % 2 == 0) {
out = 3;
} else if ((a / 2) % 2 == 1) {
out = 4;
}
} else if (a <= 2) {
out = -1;
}
System.out.println(out);
}
}
}
小天才公司福利 1166人发布
查看15道真题和解析
