Advertisement

华为机试052杨辉三角变形

阅读量:

题目描述:

1

1 1 1

1 2 3 2 1

1 3 6 7 6 3 1

1 4 10 16 19 16 10 4 1

上述所展示的三角形数列结构中,第一行仅包含一个数值为1的元素,后续每一行中的每个数值均由其正上方、左上方以及右上方三个位置的数值相加得出(若某位置不存在数值,则视作0)。请找出第n行中首个出现偶数的位置,若该行不存在偶数,则返回-1。例如,当输入为3时,输出结果为2;当输入为4时,输出结果为3。

Java实现:

复制代码
 import java.util.Scanner;

    
  
    
 public class YangHuiSanJiao {
    
     public static void main(String[] args) {
    
     Scanner sc = new Scanner(System.in);
    
     while (sc.hasNext()) {
    
         int n = sc.nextInt();
    
         System.out.println(getFirstEvenPlace(n));
    
     }
    
     }
    
  
    
     private static int getFirstEvenPl

全部评论 (0)

还没有任何评论哟~