網易內推總結

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);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章