题解 | #杨辉三角的变形#
杨辉三角的变形
https://www.nowcoder.com/practice/8ef655edf42d4e08b44be4d777edbf43
import java.io.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); if(n == 1 || 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'); } } }
使用二维数组存储三角形数阵,然后遍历最后一行,找到第一个偶数的位置即可。如果最后一行没有偶数,则返回-1
n 1 2 3 4 5 6 7 8 9 10 11 12 13 14
-1 -1 2 3 2 4 2 3 2 4 2 3 2 4