当前位置:网站首页>[written examination questions of meituan]

[written examination questions of meituan]

2022-06-13 06:08:00 mango660

It's ridiculous , I didn't expect to take the exam JAVA、C++、 Android , You think you got the basic exam ?
No , They take the thread test 、 Deadlocks, etc , And messaging .
Asking is , These questions are all muddled , My little foundation , There is no brain for dealing with these questions .

The first algorithm

  • Didn't do it , It should be greedy , But don't play games , I'm so confused .
  • Let's start with the code I used to write the questions .
  • Subsequent revisions .

subject :
Xiaomei recently fell in love with a game , There are... In the game n A monster , Each monster has a different amount of health . When fighting any monster, Xiaomei will cause damage equal to her own attack power in each round . Hit a monster's HP to less than or equal to 0 After that, the monster's experience will be absorbed by Xiaomei , Make Xiaomei's attack power increase by one . Xiaomei's initial attack power is 1, Xiaomei can decide the order of attacking monsters at will , You can also attack another monster if you have already attacked one monster without killing it . Ask Xiaomei for the minimum number of rounds to kill all monsters .

 The first line is an integer n(1≤n≤105) Represents the number of monsters .
 The second line n An integer separated by spaces ai(1≤ai≤105) Represents the health of these monsters .

function sort(arr) {
    
    for (let i = arr.length - 1; i >= 0; i --) {
    
        for (let j = 0; j <= i; j ++) {
    
            if (arr[j] > arr[j + 1]) {
    
                let temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }

    return arr;
}


function solution(arr) {
    
    let r = 1; //  Xiaomei's initial attack power .
    let count = 0;
    let n = arr.length; //  Number of monsters 
    arr = sort(arr);
    //  Array sorting 

    for (let i = 0; i < n; i ++) {
    
        let n1 = arr[i]; //  Each monster's HP 
        let diff = n1 - r;
        if (diff === 0) {
    
            //  Solve a monster in one round 
            count ++;
            r ++;
        } else {
    
            count += Math.ceil(diff / r);
            r ++;
        }
    }
    return count;
}

console.log(solution([1, 2, 3, 4, 5]));


The second way

Made it out. , however ….

Yes n Baskets , from 1 To n A colored ball is added to the basket . If a yes b The factor of , namely b It can be a to be divisible by . The number is b Will be added to the basket a All balls in . Find the number of colors of balls of different colors in the last basket .


let n = readInt();
let col = [];
let res = [];
for (let i = 0; i < n; i ++ ) {
    
    col[i] = readInt();
}

function yinshu(num) {
    
    //  Find the factorization of the number 
    let yins = [];
    for (let i = 1; i <= num; i ++) {
    
        if (num % i === 0) {
    
            yins.push(i);
        }
    }
    return yins;
}

function solution(colors) {
    
    let n = colors.length;
    let arr = [0, ...colors];
    let res = new Array(n);

    for (let i = 1; i <= n; i ++) {
    
        
        //  seek  i  The factor of 
        let yin = yinshu(i); //  Returns the basket label of the factor 

        //  Find the color corresponding to each factor 
        let yanse = [];
        for (let z = 0; z < yin.length; z ++) {
    
            yanse[z] = arr[yin[z]];
        }

        let set = [...new Set(yanse)];

        res[i - 1] = set.length;
    }

    return res;
}

res = solution(col)

for (let i = 0; i < n; i ++ ) {
    
    console.log(res[i])
}


function sushu(num) {
    
    for (let j = 2; j <= Math.sqrt(num); j ++) {
    
        if (num % j == 0) {
    
            //  Not primes 
            return false;
        } else {
    
            return true;
        }
    }
}

function yinshu(num) {
    
    //  Find the factorization of the number 
    let yins = [];
    for (let i = 1; i <= num; i ++) {
    
        if (num % i === 0) {
    
            yins.push(i);
        }
    }
    return yins;
}

function solution(colors) {
    
    let n = colors.length;
    let arr = [0, ...colors];
    let res = new Array(n);

    for (let i = 1; i <= n + 1; i ++) {
    
        // i  Only by yourself and 1 to be divisible by 
        //  Judge a number as a prime number 
        if (sushu(i)) {
    
            if (arr[1] == arr[i]) {
    
                res[i - 1] = 1;
            } else {
    
                res[i - 1] = 2;
            }
        } else {
    
            // i  With a factor 

            //  seek  i  The factor of 
            let yin = yinshu(i); //  Returns the basket label of the factor 

            //  Find the color corresponding to each factor 
            let yanse = [];
            for (let z = 0; z < yin.length; z ++) {
    
                yanse[i] = arr[yin[z]];
            }
            let set = [...new Set(yanse)];
            res[i - 1] = set.length;
            /* for (let j = 0; j <= yin.length; j ++) { let x = yin[j]; //  Basket label i if (arr[i] === arr[x]) { //  Label for x There are... In my basket   Factor the colors in the basket  } } */
        }
    }

    return res;
}


console.log(solution([1, 2, 2, 3, 5, 6]));
原网站

版权声明
本文为[mango660]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130603067310.html