题解 | #杨辉三角的变形#

杨辉三角的变形

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);
        }
    }
}
*/
全部评论

相关推荐

02-22 20:28
重庆大学 Java
程序员牛肉:首先不要焦虑,你肯定是有希望的。 首先我觉得你得好好想一想自己想要什么。找不到开发岗就一定是失败的吗?那开发岗的35岁危机怎么说?因此无论是找工作还是考公我觉得你都需要慎重的想一想。但你一定要避开这样一个误区:“我是因为找不到工作所以不得不选择考公”。 千万不要这么想。你这个学历挺好的了,因此你投后端岗肯定是有面试机会的。有多少人简历写的再牛逼,直接连机筛简历都过不去有啥用?因此你先保持自信一点。 以你现在的水平的话,其实如果想要找到暑期实习就两个月:一个月做项目+深挖,并且不断的背八股。只要自己辛苦一点,五月份之前肯定是可以找到暑期实习的,你有点太过于高看大家之间的技术差距了。不要焦虑不要焦虑。 除此之外说回你这个简历内容的话,基本可以全丢了。如果想做后端,先踏踏实实做两个项目再说+背八股再说。如果想考公,那就直接备战考公。 但是但是就像我前面说的:你考公的理由可以是因为想追求稳定,想追求轻松。但唯独不能是因为觉得自己找不到工作。不能这么小瞧自己和自己的学历。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务