英文:
Create a function that takes a locker number parameter, and returns the floor number
问题
I have a job assignment with a problem like this
There is a building with unlimited floors. Within each floor there are a number of lockers with consecutive locker numbers, in a configuration like this:
Floor. 1 there are 5 lockers, number 1-5
Floor. 2 there are 6 lockers, numbers 6-11
Floor. 3 there are 7 lockers, numbers 12-18
Floor. 4 there are 5 lockers, numbers 19-23
Floor. 5 there are 6 lockers, numbers 24-29
Floor. 6 there are 7 lockers, numbers 30-36
Etc…
Create a function that takes a locker number parameter, and returns the floor number
What's the best code to solve this question? Thank you very much.
I did this code:
const Floor = 10;
for (let i = 1; i < lantai + 1; i++) {
var lokerStart = 0;
var lokerEnd = 0;
if (i % 3 === 1) {
lokerStart = i;
lokerEnd = lokerStart + 4;
} else if (i % 3 === 2) {
lokerStart = 6;
lokerEnd = lokerStart + 5;
} else if (i % 3 === 0) {
lokerStart = 12;
lokerEnd = lokerStart + 6;
}
console.log("Floor", i, "Locker Number", `${lokerStart} - ${lokerEnd}`);
}
but it resulted in:
Floor 1, Locker Number 1 - 5
Floor 2, Locker Number 6 - 11
Floor 3, Locker Number 12 - 18
Floor 4, Locker Number 4 - 8
Floor 5, Locker Number 6 - 11
Floor 6, Locker Number 12 - 18
Floor 7, Locker Number 7 - 11
Floor 8, Locker Number 6 - 11
Floor 9, Locker Number 12 - 18
Floor 10, Locker Number 10 - 14
I want a function to get a correct floor by inputting locker number 19 for example and get floor 4.
您希望翻译的部分已提供。如果需要其他帮助,请随时告诉我。
英文:
I have a job assignment with a problem like this
There is a building with unlimited floors. Within each floor there are a number of lockers with consecutive locker numbers, in a configuration like this:
Floor. 1 there are 5 lockers, number 1-5
Floor. 2 there are 6 lockers, numbers 6-11
Floor. 3 there are 7 lockers, numbers 12-18
Floor. 4 there are 5 lockers, numbers 19-23
Floor. 5 there are 6 lockers, numbers 24-29
Floor. 6 there are 7 lockers, numbers 30-36
Etc…
Create a function that takes a locker number parameter, and returns the floor number
What's the best code to solve this question? Thank you very much.
I did this code:
const Floor= 10;
for (let i = 1; i < lantai + 1; i++) {
var lokerStart = 0;
var lokerEnd = 0;
if (i % 3 === 1) {
lokerStart = i;
lokerEnd = lokerStart + 4;
} else if (i % 3 === 2) {
lokerStart = 6;
lokerEnd = lokerStart + 5;
} else if (i % 3 === 0) {
lokerStart = 12;
lokerEnd = lokerStart + 6;
}
console.log("Floor", i, ", Locker Number", `${lokerStart} - ${lokerEnd}`);
}
but it resulted in:
Floor 1 ,Locker Number 1 - 5
Floor 2 ,Locker Number 6 - 11
Floor 3 ,Locker Number 12 - 18
Floor 4 ,Locker Number 4 - 8
Floor 5 ,Locker Number 6 - 11
Floor 6 ,Locker Number 12 - 18
Floor 7 ,Locker Number 7 - 11
Floor 8 ,Locker Number 6 - 11
Floor 9 ,Locker Number 12 - 18
Floor 10 ,Locker Number 10 - 14
I want a function to get a correct floor by inputing locker number 19 for example and get floor 4.
答案1
得分: 3
你的问题主体(你尝试的解决方案)似乎不符合你的问题:你需要创建一个接受储物柜编号作为输入参数并返回楼层的函数。
可能有一种纯数学的方法来做到这一点。与此同时,我这里的解决方案有点天真,它从第一楼开始,根据你的规则(5
、6
、7
、5
、6
、7
、5
...)逐渐添加储物柜,直到找到正确的楼层:
function getFloor(n) {
let currentFloor = 1;
let lockersArray = [5, 6, 7];
let lockersSoFar = lockersArray[(currentFloor - 1) % 3];
while (n > lockersSoFar) {
currentFloor++;
lockersSoFar += lockersArray[(currentFloor - 1) % 3];
}
return currentFloor;
};
console.log(getFloor(4))
console.log(getFloor(18))
console.log(getFloor(19))
console.log(getFloor(42))
console.log(getFloor(128))
(代码部分未翻译)
英文:
Your question body (your attempted solution) doesn't seem to match your problem: you need to create a function that gets the locker number as the input (argument) and returns the floor.
There's probably a pure mathematical way to do that. Meanwhile, my solution here is a bit more naive, it starts at the first floor and keeps adding lockers according to your rule (5
, 6
, 7
, 5
, 6
, 7
, 5
...) as we go up the floors (check the while
), until finding the correct floor:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function getFloor(n) {
let currentFloor = 1;
let lockersArray = [5, 6, 7];
let lockersSoFar = lockersArray[(currentFloor - 1) % 3];
while (n > lockersSoFar) {
currentFloor++;
lockersSoFar += lockersArray[(currentFloor - 1) % 3];
}
return currentFloor;
};
console.log(getFloor(4))
console.log(getFloor(18))
console.log(getFloor(19))
console.log(getFloor(42))
console.log(getFloor(128))
<!-- end snippet -->
答案2
得分: 3
如@GerardoFurtado所指出,确实有一种纯数学方法来解决这个问题(以下代码中的更简洁版本):
function findFloor(lockerNumber) {
const quotient = Math.floor(lockerNumber / 18);
const remainder = lockerNumber % 18;
if (remainder > 11) {
return quotient * 3 + 3;
} else if (remainder > 5) {
return quotient * 3 + 2;
} else {
return quotient * 3 + 1;
}
}
解释
首先,让我们看一下有关该建筑的ASCII可视化:
1 | # # # # #
2 | # # # # # #
3 | # # # # # # #
4 | # # # # #
5 | # # # # # #
6 | # # # # # # #
7 | # # # # #
8 | # # # # # #
9 | # # # # # # #
10 | # # # # #
11 | # # # # # #
12 | # # # # # # #
...
看到它们是如何重复的吗?每3层储物柜的数量从5增加到7,然后再次变为5。由于这些块是相同的,我们只需要找出有多少个它们。一个简单的除法就可以解决(记得向下取整到最接近的整数)。请注意,由于这是块的数量,我们需要将其乘以3以得到相应楼层的数量。
const quotient = Math.floor(lockerNumber / 18);
现在,如何找到其余的部分?你可能已经明白了:上述除法的余数。
const remainder = lockerNumber % 18;
重要部分:如果余数大于11,我们需要再爬三层楼。如果余数大于5但小于或等于11,再爬两层楼。否则,再爬一层楼。确保将它们添加到最终结果中。
if (remainder > 11) {
return quotient * 3 + 3;
} else if (remainder > 5) {
return quotient * 3 + 2;
} else {
return quotient * 3 + 1;
}
试一下:
function findFloor(lockerNumber) {
const quotient = Math.floor(lockerNumber / 18);
const remainder = lockerNumber % 18;
return quotient * 3 + 1 + Math.floor(remainder / 6);
}
for (let i = 1; i < 1e9; i += Math.random() * 1e8 | 0) {
console.log(i, findFloor(i));
}
英文:
As noted by @GerardoFurtado, there is indeed a pure mathematical way to solve this problem (a terser version can be found in the snippet below):
function findFloor(lockerNumber) {
const quotient = Math.floor(lockerNumber / 18);
const remainder = lockerNumber % 18;
if (remainder > 11) {
return quotient * 3 + 3;
} else if (remainder > 5) {
return quotient * 3 + 2;
} else {
return quotient * 3 + 1;
}
}
Explanation
First, let's have a look at an ASCII visualization of the building in question:
1 | # # # # #
2 | # # # # # #
3 | # # # # # # #
4 | # # # # #
5 | # # # # # #
6 | # # # # # # #
7 | # # # # #
8 | # # # # # #
9 | # # # # # # #
10 | # # # # #
11 | # # # # # #
12 | # # # # # # #
...
See how they repeat? Every 3 floors the number of locker goes from 5 through 7 and then it becomes 5 again. Since these blocks are the same, we just need to find how many of them are there. A simple division will do (remember to round it down to the nearest integer). Note that, since this is the number of blocks, we need to multiply it by 3 to get the number of corresponding floors.
const quotient = Math.floor(lockerNumber / 18);
Now, how to find the rest of them? You probably got it right: the remainder of the division above.
const remainder = lockerNumber % 18;
The important part: If the remainder is greater than 11, we have three floors left to climb. If it is greater than 5 but less than or equal to 11, two floors. Else, one floor left. Make sure to add them to our final result.
if (remainder > 11) {
return quotient * 3 + 3;
} else if (remainder > 5) {
return quotient * 3 + 2;
} else {
return quotient * 3 + 1;
}
Try it:
<!-- begin snippet: js hide: true console: true -->
<!-- language: lang-js -->
function findFloor(lockerNumber) {
const quotient = Math.floor(lockerNumber / 18);
const remainder = lockerNumber % 18;
return quotient * 3 + 1 + Math.floor(remainder / 6);
}
for (let i = 1; i < 1e9; i += Math.random() * 1e8 | 0) {
console.log(i, findFloor(i));
}
<!-- language: lang-css -->
.as-console-wrapper {
max-height: 100% !important;
}
<!-- end snippet -->
答案3
得分: 0
首先,你在每次循环时都重置了起始和结束值。这是导致每隔3层就丢失数值的原因。其次,你在`i%3==2`和`i%3==0`处硬编码了起始值。这导致你的数值总是从这里开始。
将初始化值移到外部,并使用变量确定下一个起始值。
```js
const lantai= 10;
var lokerStart = 0;
var lokerEnd = 0;
for (let i = 1; i < lantai + 1; i++) {
if (i % 3 === 1) {
lokerStart = lokerEnd + 1;
lokerEnd = lokerStart + 4;
} else if (i % 3 === 2) {
lokerStart = lokerEnd + 1;
lokerEnd = lokerStart + 5;
} else if (i % 3 === 0) {
lokerStart = lokerEnd + 1;
lokerEnd = lokerStart + 6;
}
console.log("楼层", i, ",储物柜编号", `${lokerStart} - ${lokerEnd}`);
}
输出:
楼层 1 , 储物柜编号 1 - 5
楼层 2 , 储物柜编号 6 - 11
楼层 3 , 储物柜编号 12 - 18
楼层 4 , 储物柜编号 19 - 23
楼层 5 , 储物柜编号 24 - 29
楼层 6 , 储物柜编号 30 - 36
楼层 7 , 储物柜编号 37 - 41
楼层 8 , 储物柜编号 42 - 47
楼层 9 , 储物柜编号 48 - 54
楼层 10 , 储物柜编号 55 - 59
<details>
<summary>英文:</summary>
A few things, firstly, you are resetting your start and end every loop. This is what causes it to drop the values every 3 floors. Secondly, you hardcoded the start values for `i%3==2` and `i%3==0`. This makes it so that your values are always starting from there.
Shift the initialising values outside and use the variables to determine the next start.
const lantai= 10;
var lokerStart = 0;
var lokerEnd = 0;
for (let i = 1; i < lantai + 1; i++) {
if (i % 3 === 1) {
lokerStart = lokerEnd + 1;
lokerEnd = lokerStart + 4;
} else if (i % 3 === 2) {
lokerStart = lokerEnd + 1;
lokerEnd = lokerStart + 5;
} else if (i % 3 === 0) {
lokerStart = lokerEnd + 1;
lokerEnd = lokerStart + 6;
}
console.log("Floor", i, ", Locker Number", ${lokerStart} - ${lokerEnd}
);
}
Outputs:
Floor 1 , Locker Number 1 - 5
Floor 2 , Locker Number 6 - 11
Floor 3 , Locker Number 12 - 18
Floor 4 , Locker Number 19 - 23
Floor 5 , Locker Number 24 - 29
Floor 6 , Locker Number 30 - 36
Floor 7 , Locker Number 37 - 41
Floor 8 , Locker Number 42 - 47
Floor 9 , Locker Number 48 - 54
Floor 10 , Locker Number 55 - 59
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论