更改现金功能优化 - Javascript [英] Change Cash Function Optimization - Javascript
问题描述
今天我来帮你优化一些代码!
几周前我被要求在测试中解决一个问题,并想出了这个非最佳解决方案,因为我开始学习 Javascript.
我想知道是否有办法(当然有)让这一切变得更好.
情况如下:
我是一个特别善良的人,拥有无限量的 2 美元、5 美元和 10 美元的钞票,我愿意为任何想要的人兑换现金,但要以最有效的方式,以最少的金额账单."
由于我必须返回一个带有账单数量的对象(如果少于$2,则返回null),我给出了以下解决方案:
function giveChange(change){变量二 = 0;变量五 = 0;变量十 = 0;var changeToGo = 改变;而(changeToGo >= 11){changeToGo -=10;十+=1;}while(changeToGo >=7 && changeToGo <=11){changeToGo-=5;五+=1;}while(changeToGo >=2 && changeToGo <=6 ){if(changeToGo == 5){changeToGo -=5;五+=1;}别的{changeToGo -= 2;二+=1;}}如果(改变<2){返回空;}返回 {二:二,五:五,十十};}
我认为必须有一个更聪明、更优雅的解决方案(可能使用 mod% 或其他东西),但那个怪物是我当时能够实现的哈哈哈.
有什么想法吗?
删除了最初的答案,因为它是基于对需求的误解.
好的,那么让我们反过来说,首先让我们确定我需要分发多少张 2 美元的钞票才能得到一个可以被 5 美元钞票整除的数字.所以现在我只关心其余的 <5 美元用于计算 2 美元的钞票.
一般的想法是,如果我有一个 change%5
为 2 的值,比如 12 美元或 17 美元或 22 美元,......我分发一张 2 美元的钞票,我可以将其余的除以5.
对于 14 美元、19 美元、... 两张 2 美元的钞票,11 美元、16 美元...我减去 6 美元,13 美元、18 美元...就是 8 美元
有了它,我就有了一张静态表格,显示每次 change % 5
时我必须分发多少张 2 美元的钞票;不需要任何循环,只需简单的查找即可.
function giveChange(change){const 二 = 改变 <4?变化>>1:[0,3,1,4,2][Math.floor(change) % 5],休息 = 变化 - 两个 * 2;返回 {二,五:Math.floor((rest%10)/5),十:Math.floor(rest/10),};}
<小时><块引用>
要再次解释一下这是做什么的吗?"change>>1" 和 "[0,3,1,4,2][Math.floor(change) % 5];"?
<代码>更改>>1 在这里只是 Math.floor(change/2)
的简写,主要处理特殊情况 giveChange(1)
和 giveChange(3)
;它们不可互换,但对于范围 0..4
(我使用它的地方)它们产生相同的结果.
让二 = [0,3,1,4,2][change % 5];//做让两个;if(change%5 === 0) 二 = 0;if(change%5 === 1) 二 = 3;if(change%5 === 2) 二 = 1;if(change%5 === 3) 二 = 4;if(change%5 === 4) 二 = 2;
和 Math.floor(change) % 5
只是为了防止你将它与浮点数一起使用,比如 giveChange(25.90)
但也许一个例子可以更好地解释它
for(let change=4; change<50; ++change){让二 = [0,3,1,4,2][change%5];console.log("变化:%i,二:%i,休息:%i",变化,二,变化 - 两个*2);}
.as-console-wrapper{top:0;max-height:100%!important}
您会看到 two
如何总是以相同的顺序具有相同的 5 个值,并且现在其余的值如何被 5 或 10 整除.
这就是这段代码的作用,它查找数组 [0,3,1,4,2]
,任何给定的 change
需要使用多少张 2 美元的钞票>,所以剩下的可以被 5 整除.
Today I come to your aid in favor of optimizing some code!
I was asked to solve a problem on a test a few weeks ago, and came up with this non-optimal solution since I'm beggining to learn Javascript.
I wonder if there's a way (of course there is) to make this better.
Here's the situation:
"I am a particular kind person who has an infinite ammount of $2, $5 and $10 dollar bills, and I'm willing to change cash for anyone who wants it, BUT in the most efficient way, with the minimum ammount of bills."
Since I had to return an object with the number of bills (or null in case of less than $2), I gave the following solution:
function giveChange(change){
var two = 0;
var five = 0;
var ten = 0;
var changeToGo = change;
while(changeToGo >= 11){
changeToGo -=10;
ten+=1;
}
while(changeToGo >=7 && changeToGo <=11){
changeToGo-=5;
five+=1;
}
while(changeToGo >=2 && changeToGo <=6 ){
if(changeToGo == 5){
changeToGo -=5;
five+=1;
}
else{
changeToGo -= 2;
two+=1;
}
}
if (change < 2){
return null;
}
return {
two: two,
five: five,
ten: ten
};
}
I think there must be a clever and more elegant solution (probably with mod% or something), but that monstrosity was what I was able to pull off at the time hahaha.
Any thoughts?
Edit: Removed he initial answer, as it was based on a misconception about the requirements.
Ok, then let's turn that around, first let's determine how many $2 bills I'll have to hand out to get to a number that's divisible by $5 bills. So for now I only care about the rests < $5 for to calculate the $2 bills.
The generic idea is, If I have a value where change%5
is 2, like $12 or $17 or $22, ... I hand out one $2 bill and I can divide the rest by 5.
For values like $14, $19, ... it's two $2 bills, for $11, $16, ... I subtract $6, and for $13, $18, ... it's $8
With that I have a static table of how many $2 bills I'd have to hand out, for every change % 5
; no loops or so needed, just a simple lookup.
function giveChange(change){
const two = change < 4? change>>1: [0,3,1,4,2][Math.floor(change) % 5],
rest = change - two*2;
return {
two,
five: Math.floor((rest%10)/5),
ten: Math.floor(rest/10),
};
}
Care to explain again what this does? "change>>1" and "[0,3,1,4,2][Math.floor(change) % 5];" ?
change >> 1
is here just a shorthand for Math.floor(change/2)
and deals mostly with the special cases giveChange(1)
and giveChange(3)
; They are not interchangable, but for the range 0..4
(where I use it) they produce the same result.
let two = [0,3,1,4,2][change % 5];
//does
let two;
if(change%5 === 0) two = 0;
if(change%5 === 1) two = 3;
if(change%5 === 2) two = 1;
if(change%5 === 3) two = 4;
if(change%5 === 4) two = 2;
And Math.floor(change) % 5
is just in case that you'll use this with floats, like giveChange(25.90)
But maybe an example explains it better
for(let change=4; change<50; ++change){
let two = [0,3,1,4,2][change%5];
console.log("change: %i, two: %i, rest: %i", change, two, change - two*2);
}
.as-console-wrapper{top:0;max-height:100%!important}
you see how two
always always has the same 5 values in the same order, and how the rest now is divisible by 5 and or 10.
That's what this code does, it looks up the Array [0,3,1,4,2]
, how many $2 bills it needs use for any given change
, so that the rest is divisible by 5.
这篇关于更改现金功能优化 - Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!