网易内推总结

来源:互联网 发布:博世配网工程设计软件 编辑:程序博客网 时间:2024/06/06 08:36

1. 斐波那契数列

Fibonacci数列是这样定义的:
F[0] = 0
F[1] = 1
for each i ≥ 2: F[i] = F[i-1] + F[i-2]
因此,Fibonacci数列就形如:0, 1, 1, 2, 3, 5, 8, 13, …,在Fibonacci数列中的数我们称为Fibonacci数。给你一个N,你想让其变为一个Fibonacci数,每一步你可以把当前数字X变为X-1或者X+1,现在给你一个数N求最少需要多少步可以变为Fibonacci数。
输入例子:
15
输出例子:
2
代码:

/**找出这个数的左边的斐波那契数和右边的斐波那契数,然后去最小的*/public class Main {    public static void main(String[] args) {        Scanner cin = new Scanner(System.in);        int a = 0;        if(cin.hasNextInt())            a = cin.nextInt();        int fisrt = 0;        int second = 1;        int left = 0;        int right = 0;        while(true) {            int sum = fisrt + second;            fisrt = second;            second = sum;            if(sum < a) {                left = a - sum;            }else {                right = sum - a;                break;            }        }        System.out.println(Math.min(left, right));    }}

2. 找到最小的不能合成的数

小易邀请你玩一个数字游戏,小易给你一系列的整数。你们俩使用这些整数玩游戏。每次小易会任意说一个数字出来,然后你需要从这一系列数字中选取一部分出来让它们的和等于小易所说的数字。 例如: 如果{2,1,2,7}是你有的一系列数,小易说的数字是11.你可以得到方案2+2+7 = 11.如果顽皮的小易想坑你,他说的数字是6,那么你没有办法拼凑出和为6 现在小易给你n个数,让你找出无法从n个数中选取部分求和的数字中的最小数

代码:

public class Main {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        int a = 0;        if(scanner.hasNextInt())            a = scanner.nextInt();        int[] array = new int[a];        for(int i = 0; i < array.length; i++) {            array[i] = scanner.nextInt();        }        Arrays.sort(array); //先排序        int miss = 0; //0 - miss之内都是可以合成的        for(int i = 0; i < array.length; i++) {            if(array[i] > miss+1) //每次加1  如果大于表示断开了                break;            miss += array[i];        }        System.out.println(miss+1);    }}

3. 字符串问题

小易喜欢的单词具有以下特性:
1.单词每个字母都是大写字母
2.单词没有连续相等的字母
3.单词没有形如“xyxy”(这里的x,y指的都是字母,并且可以相同)这样的子序列,子序列可能不连续。
例如:
小易不喜欢”ABBA”,因为这里有两个连续的’B’
小易不喜欢”THETXH”,因为这里包含子序列”THTH”
小易不喜欢”ABACADA”,因为这里包含子序列”AAAA”
小易喜欢”A”,”ABA”和”ABCBA”这些单词
给你一个单词,你要回答小易是否会喜欢这个单词。

输入例子:
AAA

输出例子:
Dislikes

public class Main {    public static void main(String args[]) {        Scanner cin = new Scanner(System.in);        String str = null;        if(cin.hasNextLine()) {            str = cin.nextLine();        }        if(isAllUpperCase(str) && notContinuousEqual(str) && notXYXYEqual(str)) {            System.out.println("Likes");        }else {            System.out.println("Dislikes");        }    }    //匹配大写字母    public static boolean isAllUpperCase(String s) {        return s.matches("[A-Z]+");    }    //不连续相等    public static boolean notContinuousEqual(String s) {        return !s.matches(".*(.)(\\1).*");    }    //Not XYXY    public static boolean notXYXYEqual(String s) {        return !s.matches(".*(.).*(.).*(\\1).*(\\2).*");    }}

4. 字符串排序

考拉有n个字符串字符串,任意两个字符串长度都是不同的。考拉最近学习到有两种字符串的排序方法: 1.根据字符串的字典序排序。例如:
“car” < “carriage” < “cats” < “doggies < “koala”
2.根据字符串的长度排序。例如:
“car” < “cats” < “koala” < “doggies” < “carriage”
考拉想知道自己的这些字符串排列顺序是否满足这两种排序方法,考拉要忙着吃树叶,所以需要你来帮忙验证。

输入例子:
3
a
aa
bbb
输出例子:
both

代码:

public class Main {    public static void main(String args[]) {        Scanner cin = new Scanner(System.in);        int count = 0;        if(cin.hasNextInt()) {            count = cin.nextInt();        }        cin.nextLine();        String[] words = new String[count];        for(int i = 0; i < words.length; i++) {            words[i] = cin.nextLine();        }        if(isSortByChar(words) && isSortByLength(words)) {            System.out.println("both");        }else if(isSortByLength(words)) {            System.out.println("lengths");        }else if(isSortByChar(words)) {            System.out.println("lexicographically");        }else {            System.out.println("none");        }    }    public static boolean isSortByLength(String[] words) {        int i = 0;        int len = words.length;        //通过长度来判断        while(i < (len-1)) {            if(words[i].length() < words[i+1].length()) {                i++;                continue;            }            return false;        }        return true;    }    public static boolean isSortByChar(String[] words) {        int i = 0;        int len = words.length;        while(i < (len-1)) {            //通过字母顺序  compareTO < 0            if(words[i].compareTo(words[i+1]) < 0) {                i++;                continue;            }            return false;        }        return true;    }}

5. 幸运数的个数

一个袋子里面有n个球,每个球上面都有一个号码(拥有相同号码的球是无区别的)。如果一个袋子是幸运的当且仅当所有球的号码的和大于所有球的号码的积。
例如:如果袋子里面的球的号码是{1, 1, 2, 3},这个袋子就是幸运的,因为1 + 1 + 2 + 3 > 1 * 1 * 2 * 3
你可以适当从袋子里移除一些球(可以移除0个,但是别移除完),要使移除后的袋子是幸运的。现在让你编程计算一下你可以获得的多少种不同的幸运的袋子。

输入例子:
3
1 1 1
输出例子:
2

解法:
数字从小到大排序,然后假设你已经选择了若干个数,并且这些数的 和不大于积 ,那么再往其中添加一个 >1 的数,必定还是 和不大于积。但是往里边添加 1,就不一样了。所以如果出现 和不大于积 的情况,那么如果当前数字不是1,就没有必要往后递归去找了。

public class Main {    public static void main(String[] args) {        Scanner cin = new Scanner(System.in);        int count = 0;        if(cin.hasNextInt()) {            count = cin.nextInt();        }        int[] array = new int[count];        for(int i = 0; i < array.length; i++) {            array[i] = cin.nextInt();        }        Arrays.sort(array);        System.out.println(findLuckyCount(array, 0, 0, 1));    }    public static int findLuckyCount(int[] array, int index, int sum, int multi) {        int count = 0;        for(int i = index; i < array.length; i++) {            sum += array[i];            multi *= array[i];            if(sum > multi) {                count += (1 + findLuckyCount(array, i+1, sum, multi));            }else if(array[i] == 1) { //可能是Lucy数                count += findLuckyCount(array, i+1, sum, multi);            }else                break;  // sum <= multi且array[i]不等于1 后面剪掉            sum -= array[i];            multi /= array[i];            //去重            while(i < (array.length-1) && array[i] == array[i+1]) {                i++;            }        }        return count;    }}

6.回文字符串的判断

“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。花花非常喜欢这种拥有对称美的回文串,生日的时候她得到两个礼物分别是字符串A和字符串B。现在她非常好奇有没有办法将字符串B插入字符串A使产生的字符串是一个回文串。你接受花花的请求,帮助她寻找有多少种插入办法可以使新串是一个回文串。如果字符串B插入的位置不同就考虑为不一样的办法。
例如:
A = “aba”,B = “b”。这里有4种把B插入A的办法:
* 在A的第一个字母之前: “baba” 不是回文
* 在第一个字母‘a’之后: “abba” 是回文
* 在字母‘b’之后: “abba” 是回文
* 在第二个字母’a’之后 “abab” 不是回文
所以满足条件的答案为2

输入例子:
aba
b
输出例子:
2

public class Main {    public static void main(String args[]) {        Scanner cin = new Scanner(System.in);        String[] words = new String[2];        for(int i = 0; i < words.length; i++) {            words[i] = cin.nextLine();        }        //reverse之后还是相等  回文字符串        System.out.println(getPalindromeCount(words[0], words[1]));    }    public static int getPalindromeCount(String a, String b) {        int count = 0;        for(int i = 0; i <= a.length(); i++) {            String temp = a.substring(0, i) + b + a.substring(i);            StringBuilder sb = new StringBuilder(temp); //需要用到reverse方法            if(sb.reverse().toString().equals(temp)) {                count++;            }        }        return count;    }}

7. 格子距离不为2

二货小易有一个W*H的网格盒子,网格的行编号为0~H-1,网格的列编号为0~W-1。每个格子至多可以放一块蛋糕,任意两块蛋糕的欧几里得距离不能等于2。
对于两个格子坐标(x1,y1),(x2,y2)的欧几里得距离为:
( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) 的算术平方根
小易想知道最多可以放多少块蛋糕在网格盒子里。

输入例子:
3 2
输出例子:
4

代码:

//找规律public class Main {    public static void main(String args[]) {        Scanner cin = new Scanner(System.in);        int a = cin.nextInt();        int b = cin.nextInt();        int count = 0;        //能被4整除        if(a % 4 == 0 || b % 4 == 0) {            count = (a*b)/2;        }else if(a % 2 == 0 || b % 2 == 0) { //被2整除            count = ((a*b)/4 + 1)*2;        }else { //其他情况            count = (a*b)/2 + 1;        }        System.out.println(count);    }}

8. 饥饿的小易

小易总是感觉饥饿,所以作为章鱼的小易经常出去寻找贝壳吃。最开始小易在一个初始位置x_0。对于小易所处的当前位置x,他只能通过神秘的力量移动到 4 * x + 3或者8 * x + 7。因为使用神秘力量要耗费太多体力,所以它只能使用神秘力量最多100,000次。贝壳总生长在能被1,000,000,007整除的位置(比如:位置0,位置1,000,000,007,位置2,000,000,014等)。小易需要你帮忙计算最少需要使用多少次神秘力量就能吃到贝壳。
输入描述:
输入一个初始位置x_0,范围在1到1,000,000,006
输入:125000000

输出描述:
输出小易最少需要使用神秘力量的次数,如果使用次数使用完还没找到贝壳,则输出-1
输出: 1

代码:

//暴力法public class Main {    public static void main(String args[]) {        Scanner cin = new Scanner(System.in);        long num = 0;        if(cin.hasNextLong()) {            num = cin.nextLong() % 1000000007;        }        if(num == 0)            System.out.println(0); //2346123123        HashMap<Long, Integer> map = new HashMap<>();        ArrayDeque<Long> queue = new ArrayDeque<>();        map.put(num, 1);        queue.offerLast(num);        int maxCount = 100000;        long key1 = 0;        while(!queue.isEmpty()) {            key1 = queue.pollFirst();            if(key1 == 0)                break;            if(map.get(key1) > maxCount)                continue;            long key2 = ((key1<<2) + 3) % 1000000007;            if(map.get(key2) == null) {                queue.offerLast(key2);                map.put(key2, map.get(key1)+1);            }            key2 = ((key1<<3) + 7) % 1000000007;            if(map.get(key2) == null) {                queue.offerLast(key2);                map.put(key2, map.get(key1)+1);            }        }        int count = queue.isEmpty() ? -1 : map.get(key1)-1;        System.out.println(count);    }}
0 0
原创粉丝点击