题解 | #杨辉三角的变形#
杨辉三角的变形
https://www.nowcoder.com/practice/8ef655edf42d4e08b44be4d777edbf43
import java.util.Scanner; /** 杨辉三角的变形,找规律。 输入:一个正数n表示杨辉三角的第n行 输出:第n行第一个偶数出现的位置 思路:通过观察发现,1,2行没有偶数,从第三行开始,第一个偶数出现的位置的规律依次是2324循环 即:如果n-3 mod 4 == 0 2 n-4 mod 4 ==0 3 n-5 mod 4 == 0 2 n-6 mod 4 == 0 4 */ public class Main{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.close(); System.out.println(findresult(n)); } public static int findresult(int n){ int result=-1; if(n==1 || n==2) result = -1; else if((n-3) %4 == 0){ result = 2; }else if((n-4) % 4 ==0){ result = 3; }else if((n-5)%4 ==0){ result = 2; }else if((n-6)%4==0){ result = 4; } return result; } }