题解 | #杨辉三角的变形#
杨辉三角的变形
http://www.nowcoder.com/practice/8ef655edf42d4e08b44be4d777edbf43
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
int n = scan.nextInt();
if (n == 1 || n == 2) {
System.out.println(-1);
}
else if (n % 2 != 0) {
System.out.println(2);
}
else {
if (n % 4 == 0) {
System.out.println(3);
}
else System.out.println(4);
}
}
}
}
/**
* 以下是通过 11/12的代码
*/
/*通过不了最后一个case 但是下面这个方法可以把完整的三叉杨辉三角print出来
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
int max = 0;
List<Integer> list = new ArrayList<>();
int n = scan.nextInt();
list.add(n);
max = Math.max(max, n);
List<List<Integer>> res = new ArrayList<>();
solution(max, res);
for (int i : list) {
getPrint(i, res);
}
}
}
public static void getPrint(int n, List<List<Integer>> res) {
List<Integer> cur = res.get(n - 1);
for (int i = 0; i < cur.size(); i++) {
if (cur.get(i) % 2 == 0) {
System.out.println(i + 1);
return;
}
}
System.out.println(-1);
}
public static void solution(int max, List<List<Integer>> res) {
res.add(new ArrayList<>(Arrays.asList(1)));
if (max == 1) return;
for (int i = 2; i <= max; i++) {
List<Integer> cur = new ArrayList<>();
List<Integer> pre = res.get(res.size() - 1);
int len = pre.size();
for (int j = 0; j < len + 2; j++) {
int number = 0;
if (j - 2 >= 0 && j - 2 < len) number += pre.get(j - 2);
if (j - 1 >= 0 && j - 1 < len) number += pre.get(j - 1);
if (j >= 0 && j < len) number += pre.get(j);
cur.add(number);
}
//每次添加每一行的list
res.add(cur);
}
}
}
*/