美團(3.26) & 招行信用卡中心(3.27) Java開發崗位筆試實錄

美團

1. 第一題

題目描述:

首先給出你一個整數,可能爲正也可能爲負,這個數字中僅包含數字1-9,
現在定義一個1-9的置換,即指定將整數中的某個數字按順序變換爲另一個數字,
請你輸出變換以後的數字是多少。

輸入輸出:

輸入第一行包含一個整數a。(-101000<=a<=101000)
輸入第二行包含9個以空格隔開的整數a_i , 第i個整數表示將數字i替換爲數字a_i。(1<=a_i<=9)

樣例輸入輸出:

樣例輸入:
1234567
9 8 7 6 5 4 3 2 1
樣例輸出:
9876543

參考思路:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        boolean flag = true;// 如果是正數就是true,負數就是false

        String Num = cin.nextLine();
        if (Num.charAt(0) == '-') {
            flag = false;
            Num = Num.substring(1);
        }


        String numMap = cin.nextLine();
        String[] List = numMap.split(" ");

        String result = "";
        for (int index = 0; index < Num.length(); index++){
            int I = Num.charAt(index) - '0';
            result += List[I - 1];
        }

        if (!flag)
            result = "-" + result;
        System.out.println(result);

    }
}

2. 第二題

題目描述:

有這麼一個奇怪的符號:在一張正方形的紙上,有許多不同半徑的圓。他們的圓心都在正方形的重心上(正方形的重心位於正方形兩條對角線的交叉點上)。
最大的圓的外面部分是白色的。最外層的圓環被塗成了黑色,接下來第二大的圓環被塗層白色,接下來第三大的圓環被塗層黑色。以此類推,例如下圖。
現在,給你這些圓的半徑,請問黑色部分的面積是多少?精確到小數點後5位輸出(四捨五入)。

輸入:

輸入包含兩行。第一行一個整數n,表示圓的個數。
接下來n個整數,描述每個圓的半徑ri。數據保證沒有兩個圓的半徑是一樣的。(1<=n<=100 , ri<=1000)

他那個圓就類似這樣:

參考思路:

  • 我拿到手第一感覺,這題是要按照圓的個數是單還是雙分情況做;後來想想好像不太對。首先環形的面積是根據大圓的面積-小圓的面積,也就是:(R外-R內)*(R外+R內)*PI
  • 題目要求從最外面一個圓開始計算起往內逐步計算,也就是兩兩計算。但是題目還有一個要求:最小的一個圓就自動變成一個圈,不需要計算圓環的面積。我在考慮,可不可以也把它看成是大圓的面積-小圓的面積,可以!可以看作小圓的面積爲0,這樣計算起來就非常方便了。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class test2 {
    static Double PIE = Math.PI;

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int num1 = cin.nextInt();
        cin.nextLine();
        String nums = cin.nextLine();
        ArrayList<Integer> NumArray = new ArrayList<>();
        String[] numlist = nums.split(" ");
        for (int i = 0; i < numlist.length; i ++){
            NumArray.add(Integer.parseInt(numlist[i]));
        }
        Collections.sort(NumArray);

        NumArray.add(0,0);

        double result = 0.0;
        for (int i = num1; i > 0; i-= 2){
            int Rx = NumArray.get(i);
            int Ry = NumArray.get(i - 1);
            result += PIE*(Rx - Ry)*(Rx + Ry);
        }

        System.out.println(String.format("%.5f",result));
    }

}

3. 第三題

題目描述:

一個序列是有趣的需要滿足:當且僅當這個序列的每一個元素ai 滿足 i 整除ai 。
現在給定一個長度爲n的數組,問這個數組有多少個非空的子序列是有趣的,由於答案可能比較大,只需要輸出在模998244353意義下答案的就行了。
一個長度爲n的數組的非空子序列定義爲從這個數組中移除至多n-1個元素後剩下的元素有序按照原順序形成的數組。比如說對於數組[3,2,1],
它的非空子序列有[3],[2],[1],[3,2],[3,1],[2,1],[3,2,1]。

輸入輸出:

輸入:
第一行一個整數n表示序列的長度。(1<=n<=1e5)
第二行n個整數表示給定的序列。(1<=ai<=1e6)
輸出:
輸出一個數表示有趣的子序列的個數。

樣例輸入輸出:

樣例輸入:
2
3 1
樣例輸出:
2

參考思路:

  • 這題關鍵在於如何得出所有的子序列。我是用遞歸解的,感覺有點醜……
  • 如果A是B的子序列,C是B的子序列,那麼C一定是A的子序列。
import java.util.ArrayList;
import java.util.Scanner;

public class test3 {
    static boolean Judge(ArrayList<Integer> list){
        for (int i = 0; i < list.size(); i ++){
            if (list.get(i)%(i+1) != 0)
                return false;
        }
        return true;
    }

    static ArrayList<ArrayList<Integer>> subListCollection = new ArrayList<>();
    static int result = 0;

    static void getSubString(ArrayList<Integer> array) {
        if (array.size() == 1)
            return;
        else {
            for (int i = 0; i < array.size(); i++) {
                ArrayList<Integer> temp =(ArrayList<Integer>)array.clone();
                temp.remove(i);
                if (!subListCollection.contains(temp))
                    subListCollection.add(temp);
                if (Judge(temp))
                    result++;
                getSubString(temp);
            }
        }
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int N = cin.nextInt();
        cin.nextLine();
        String[] Nums2 = cin.nextLine().split(" ");
        ArrayList<Integer> NumList = new ArrayList<>();
        for (int i = 0; i < Nums2.length; i ++){
            NumList.add(Integer.valueOf(Nums2[i]));
        }

        getSubString(NumList);
        int result1 = 0;
        for (ArrayList<Integer> a : subListCollection){
            System.out.println(a);
            if (Judge(a))
                result1++;
        }
        System.out.println(result1);
        //System.out.println(result1);
    }
}

4. 第四題

題目描述:

小倉酷愛射擊運動。今天的小倉會進行 N 輪射擊,已知第 i 次射擊,她擊中靶心的概率是 a[i] -1 。
小倉想知道 N 輪射擊後,偏離靶心次數爲 0 ,1 ,2 次的概率分別是多少。

輸入:

第一行一個數 N,代表射擊輪數。
第二行 N 個數 a[i],第 i 個數爲 a[i]。
1≤N≤100,000
1≤a[i]<998244353

輸出:

不難證明答案可以表示成一個最簡分數 p * q -1。
你需要輸出三個 p * q -1 對 998244353 取模後的結果,以此來表示偏離靶心次數爲 0 , 1 , 2 時的概率。
其中 q-1 是 q 在模意義下的逆元,滿足 q-1* q = 1 mod 998244353。
例如 1/4, 可以寫成 748683265,原因是 4 * 748683265 % 998244353 = 1,也有且僅有 x = 748683265,1 <= x < 998244353 滿足乘積等於 1

樣例輸入:

2
2 2

樣例輸出

748683265 499122177 748683265

參考思路:

  • 未AC,今天想想。可以私信我探討

5. 第五題

一個關於套娃的瘋狂題目。未AC。今天想想。
在這裏插入圖片描述
在這裏插入圖片描述

招行

  • 招行的筆試搞得花裏胡哨,分爲選擇題、編程題,甚至還有附加題。選擇題一共20條,佔總分的46分;編程題共2道,佔總分的54道。附加題40分,並不是很硬核,甚至還有重複的;估計是爲了讓前面寫完的同學能夠儘量更好的表現自己,根據自身的特長來解答不同領域的附加題。
  • 題目在寫的時候沒法複製,所以招行的筆試編程題題目描述只能記個大概。

1. 第一題:

題目描述:

題目大意就是,一個人他要去參加一個party,他需要出門。他家裏有n種顏色的襪子,然後分別輸入這n種顏色的襪子分別有多少隻。然後他問你,至少需要拿多少雙襪子才能保證這其中一定有兩雙顏色一樣的襪子?

輸入:

N (有N組數據)
n(襪子顏色的數量)
a1, a2, … , an(這n種顏色的襪子分別有多少隻)
n(襪子顏色的數量)
a1, a2, … , an(這n種顏色的襪子分別有多少隻)
n(襪子顏色的數量)
a1, a2, … , an(這n種顏色的襪子分別有多少隻)

輸出:

對於每組數據,輸出至少需要多少隻襪子;如果不可能實現,就輸出-1。

參考思路:

  • 先分析傳進來的數據。它比較坑的一點就是,有可能某種顏色的襪子是0只(?),所以不能單純的根據傳進來的顏色的組數計算。
  • 考慮最壞的情況,就是每種顏色的襪子都拿不到2只——0只的拿0只,其餘所有的襪子都只拿了1只;那麼下一隻,一定會拿到符合條件的襪子。
import java.util.ArrayList;
import java.util.Scanner;
public class Test1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        in.nextLine();//讀掉換行
        for (int i = 0; i < T; i++){
            int n = in.nextInt();
            in.nextLine();
            ArrayList<Integer> sockArray = new ArrayList<>();
            for (int k = 0; k < n; k++){
                sockArray.add(in.nextInt());
            }
            in.nextLine();
            cal(n,sockArray);
        }

    }

    static public void cal(int n, ArrayList<Integer> array){
        for (int i = 0; i < n; i++){
            if (array.get(i) > 1)
                break;
            if (i == n-1) {
                System.out.println("-1");
                return;
            }
        }
        int result = 0;
        int result2 = 0;
        for (int i = 0; i < n; i++){
            if (array.get(i) == 0)
                continue;
            if (array.get(i) == 1)
                result ++;
            else
                result2++;
        }
        System.out.println(result+result2+1);
    }
}

2. 第二題:

題目描述:

有一堆木頭需要處理;原本每處理一根木頭都需要花費1元;但是如果伐木工發現處理的下一根木頭的長度和寬度都>=當前的木頭的長度和寬度,那麼下一跟木頭就不收錢(?迷惑行爲)
給你一組數據,問你怎麼排列木頭的順序,才能得到花費最少的方案?

輸入:

N (數據的組數)
(長度list
寬度list)*N

輸出:

對於每一組數據,輸出花費最少的方案的花費。

參考思路:

  • 我是先把數據讀進來之後簡單做一個按照長度的排序,讓他們處理起來更方便;
  • 貪婪算法,對於當前處理的木頭,直接找到隊列中下一根長度、寬度都比它大的木頭,並且將向後遍歷過程中找到的不符合條件的木頭全部放到隊列末尾。這樣就能找到最優方案(雖然效率差了點)
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Test2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        in.nextLine();//讀掉換行
        for (int i = 0; i < T; i++){
            int n = in.nextInt();
            in.nextLine();
            ArrayList<Wood> Woods = new ArrayList<>();
            for (int k = 0; k < n; k++){
                Wood temp = new Wood();
                temp.setLength(in.nextInt());
                Woods.add(temp);
            }
            in.nextLine();
            for (int j = 0; j < n; j++){
                Woods.get((j)).setWeight(in.nextInt());
            }
            cal(Woods);
            in.nextLine();
        }
    }

    static public void cal(ArrayList<Wood> woodsArray) {
        if (woodsArray.size() == 1)
        {
            System.out.println("1");
            return;
        }
        Collections.sort(woodsArray);
        for (int j = 0; j < woodsArray.size() - 1; j++) {
            for (int i = j+1; i < woodsArray.size(); i++) {
                Wood w1 = woodsArray.get(j);
                Wood w2 = woodsArray.get(i);
                if (w2.getWeight() >= w1.getWeight()) {
                    woodsArray.add(j + 1, w2);
                    woodsArray.remove(i+1);
                    break;
                }
            }
        }
        int result = 1;
        for (int i = 1; i < woodsArray.size(); i ++){
            Wood w1 = woodsArray.get(i-1);
            Wood w2 = woodsArray.get(i);
            if (w2.getLength()>=w1.getLength() && w2.getWeight()>=w1.getWeight())
                continue;
            else
                result++;
        }
        System.out.println(result);
    }
}

class Wood implements Comparable {
    private int length;
    private int weight;

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    @Override
    public int compareTo(Object o) {
        Wood B = (Wood) o;
        if (this.getLength() > B.getLength())
            return 1;
        if (this.getLength() < B.getLength())
            return -1;
        else
        {
            if (this.getWeight() > B.getWeight())
                return 1;
            else return -1;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章