当前位置:网站首页>【美团笔试题】
【美团笔试题】
2022-06-13 06:03:00 【mango660】
就离谱,没想到考了 JAVA、C++、安卓,你以为考了基础?
不,他们考的是线程、死锁等等,以及消息传递。
问就是,这些题全是懵的,我那点基础,应付这些提根本没有头脑。
第一道算法
- 没做出来,应该是到贪心,但是不打游戏,我很懵逼。
- 先放我做题时的代码。
- 后续修订。
题目:
小美最近迷上了一款游戏,在游戏中有n个怪物,每个怪物的血量各不相同。和任何一个怪物作战时小美每个回合都会造成等同于自己攻击力的伤害。把一个怪物的血量打到小于等于0后这个怪物的经验就会被小美吸收,使得小美的攻击力加一。小美的初始攻击力是1,小美可以任意决定攻击怪物的顺序,也可以在已经攻击一个怪物且并未击杀的情况下攻击另外一个怪物。求小美击杀所有怪物的最小回合数。
第一行一个整数n(1≤n≤105)代表怪物的数量。
第二行n个空格隔开的整数ai(1≤ai≤105)代表这些怪物的血量。
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; // 小美初始攻击力。
let count = 0;
let n = arr.length; // 怪物数量
arr = sort(arr);
// 数组排序
for (let i = 0; i < n; i ++) {
let n1 = arr[i]; // 每个怪物的血量
let diff = n1 - r;
if (diff === 0) {
// 一个回合就解决一个怪物
count ++;
r ++;
} else {
count += Math.ceil(diff / r);
r ++;
}
}
return count;
}
console.log(solution([1, 2, 3, 4, 5]));
第二道
做出来了, 但是…。
有n个篮子,从1到n的篮子中都加入了一个带颜色的球。如果 a 是b的因数,即b能够被a整除。则编号为b的篮子中会再加入a中的所有球。求最后所有篮子里不同颜色的球的颜色数。
let n = readInt();
let col = [];
let res = [];
for (let i = 0; i < n; i ++ ) {
col[i] = readInt();
}
function yinshu(num) {
// 求该数字的因数分解式
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 ++) {
// 求 i 的因数
let yin = yinshu(i); // 返回的是因数的篮子标号
// 求出每个因数对应的颜色
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) {
// 不是素数
return false;
} else {
return true;
}
}
}
function yinshu(num) {
// 求该数字的因数分解式
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 只能被自己和1整除
// 判断一个数为素数
if (sushu(i)) {
if (arr[1] == arr[i]) {
res[i - 1] = 1;
} else {
res[i - 1] = 2;
}
} else {
// i 有因数的情况
// 求 i 的因数
let yin = yinshu(i); // 返回的是因数的篮子标号
// 求出每个因数对应的颜色
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]; // 篮子标号i if (arr[i] === arr[x]) { // 标号为x的篮子中有 因数篮子中的颜色 } } */
}
}
return res;
}
console.log(solution([1, 2, 2, 3, 5, 6]));
边栏推荐
- Leetcode- longest continuous increasing sequence - simple
- [automated test] cypress manual
- A glimpse of [wechat applet]
- Working principle of sentinel series (concept)
- The Boys x PUBGMOBILE 联动火热来袭!来看最新游戏海报
- Integration of sentinel series Nacos to realize rule synchronization and persistence
- Leetcode- intersection of two arrays - simple
- Power simple of leetcode-3
- You still can't remotely debug idea? Come and have a look at my article. It's easy to use
- Mongodb Multi - field Aggregation group by
猜你喜欢
[turn] explain awk (1)__ Awk Basics_ Options_ Program segment parsing and examples
Sentinel series integrates Nacos and realizes dynamic flow control
16 the usertask of a flowable task includes task assignment, multi person countersignature, and dynamic forms
为什么那么多人讨厌A-Spice
The reason why the process cannot be shut down after a spark job is executed and the solution
1+1>2,Share Creators可以帮助您实现
[spark]spark introductory practical series_ 8_ Spark_ Mllib (lower)__ Machine learning library sparkmllib practice
The difference between the increment and decrement operators before and after variables i+, +i, I –, – I
17 servicetask of flowable task
Mongodb multi field aggregation group by
随机推荐
Swift property property
18 flowable task manualtask and receivetask
2021.9.30 learning log -postman
Minimum spanning tree (prim+kruskal) learning notes (template +oj topic)
JNDI configuration for tongweb7
Explanation of service registration and discovery API of Nacos series
Software testing - Summary of common interface problems
Function and application scenario of field setaccessible() method
Shardingsphere JDBC < bind table > avoid join Cartesian product
Etcd fast cluster building
Feel the power of shardingsphere JDBC through the demo
php redis 制作高迸发秒杀
After MySQL is installed, enter the "net start MySQL" command, and an error is reported that "net" is neither an internal or external command nor a runnable program
Implementation of concurrent programming locking
Zero copy technology
Leetcode longest harmonic subsequence simple
OpenGL马赛克(八)
The difference between the increment and decrement operators before and after variables i+, +i, I –, – I
安全基线检查脚本—— 筑梦之路
Tongweb crawl performance log script