当前位置:网站首页>[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]));
边栏推荐
- Concurrent programming -- what is threading?
- Echart柱状图:echart实现堆叠柱状图
- Introduction to USB learning (I) -- Dongfeng night flower tree
- Ffmpeg download suffix is Video files for m3u8
- Software testing - Summary of common interface problems
- Leetcode- distribute cookies - simple
- Uni app provincial and urban linkage
- 軟件測試——接口常見問題匯總
- Multiple reception occurs in the uniapp message delivery
- USB 0xc0000011 error
猜你喜欢
软件测试——接口常见问题汇总
Sqlplus connection failure
杨辉三角形详解
How MySQL optimizes the use of joint index ABC
Echart line chart: different colors are displayed when the names of multiple line charts are the same
Ffmpeg download suffix is Video files for m3u8
华为开发者认证与DevEco Studio编译器下载
js将文本转成语言播放
How to view tongweb logs correctly?
What happens when the MySQL union index ABC encounters a "comparison operator"?
随机推荐
Exception after repeated application redeployment on tongweb: application instance has been stopped already or outofmemoryerror:metaspace
Function and application scenario of field setaccessible() method
Kotlin learning notes (1)
Alibaba cloud OSS file download cannot be resumed at a breakpoint
The title of the WebView page will be displayed in the top navigation bar of the app. How to customize
@The detailed explanation of configurationproperties and the problem that all properties of the entity bean modified by this annotation are null after injection are solved
arrayList && linkedList
Fusionpbx installation - road to dream
Fichier local second Search Tool everything
[spark]spark introductory practical series_ 8_ Spark_ Mllib (upper)__ Introduction to machine learning and sparkmllib
Leetcode- reverse vowels in string - simple
Rk3399 hid gadget configuration
Echart line chart: different colors are displayed when the names of multiple line charts are the same
Power simple of leetcode-3
php redis 制作高迸发秒杀
Concurrent programming -- countdownlatch combined with thread pool
Uniapp hides the scroll bar of scroll view
Leetcode perfect number simple
Experience of redis installation under Linux system (an error is reported at the same time. The struct redis server does not have a member named XXXX)
The technical analysis of ERP systems of the two camps in the world has been picked up many times.