2

#yyds干货盘点# 解决华为机试:杨辉三角的变形

 2 years ago
source link: https://blog.51cto.com/u_15488507/5080774
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

#yyds干货盘点# 解决华为机试:杨辉三角的变形

原创

97的风 2022-03-08 09:51:02 博主文章分类:面试题 ©著作权

文章标签 代码实现 数据 java 文章分类 Java 编程语言 阅读数868

1.简述:

#yyds干货盘点# 解决华为机试:杨辉三角的变形_java

以上三角形的数阵,第一行只有一个数1,以下每行的每个数,是恰好是它上面的数、左上角数和右上角的数,3个数之和(如果不存在某个数,认为该数就是0)。

求第n行第一个偶数出现的位置。如果没有偶数,则输出-1。例如输入3,则输出2,输入4则输出3,输入2则输出-1。

数据范围: 1 \le n \le 10^9 \1≤n≤109 

输入描述:

输入一个int整数

输出描述:

输出返回的int值

复制输出:

2.代码实现:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int num = in.nextInt();
if(num == 1 || num == 2){
System.out.println(-1);
continue;
}
else if(num % 4 == 1 || num % 4 == 3){
System.out.println(2);
continue;
}
else if(num % 4 == 0){
System.out.println(3);
continue;
}
else if(num % 4 == 2){
System.out.println(4);
continue;
}
}
}
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK