gpt4 book ai didi

javascript - 背包表示

转载 作者:行者123 更新时间:2023-11-30 00:27:05 25 4
gpt4 key购买 nike

Use reduce to compute the area of the largest rectangle in an array of rectangles. Each rectangle has width and height properties, and if r is a rectangle, the area is r.width * r.height

If there are no rectangles, return 0, so the base case is 0. In the online instruction examples, reduce() was used to form the sum over a set of items. In this case you will want to take the maximum over a set of items (using JavaScript’s Math.max(a, b) function.)

You must use a function named area to compute and return the area of a rectangle. It takes one parameter which is a rectangle object.

You must also define a function to pass into reduce, analogous to the sum_value function in “The Real Reduce” sub-lessons within the “Knapsack: Representation” lesson. You must name this function biggest.

下面的代码是类(class)练习的解决方案。

To complete this exercise, you should modify the code in the window below. Make the following changes:

  • Define area(rect) to compute and return the area of a rectangle object.
  • Define biggest to be the third parameter to reduce (already defined).
  • Finish the definition of compute using a call to reduce.
<html>
<head>
<title>Using Reduce</title>
<link rel="stylesheet" href="../css/exercise.css"></link>
<script>
function reduce(a, base, f) {
var result = base;
for (var i = 0; i < a.length; i++)
result = f(result, a[i]);
return result;
}

// these are the rectangles to consider
r1 = {width: 9, height: 34}; // area is 306
r2 = {width: 10, height: 31}; // area is 310
r3 = {width: 11, height: 28}; // area is 308
r4 = {width: 12, height: 25}; // area is 300
r5 = {width: 13, height: 24}; // area is 312
r6 = {width: 14, height: 22}; // area is 308
r7 = {width: 15, height: 20}; // 300
r8 = {width: 16, height: 19}; // 304
r9 = {width: 17, height: 18}; // 306
r10 = {width: 18, height: 17}; // 306
r11 = {width: 19, height: 16}; // 304
r12 = {width: 20, height: 15}; // 300
r13 = {width: 22, height: 14}; // 308
r14 = {width: 24, height: 12}; // 288
r15 = {width: 28, height: 11}; // 308

// form an array of all the rectangles
rectangles = [r1, r2, r3, r4, r5, r6, r7, r8,
r9, r10, r11, r12, r13, r14, r15];

// define the function named area here:
function area(r){

return r.width * r.height;

}

// define the function named biggest here:
function biggest(biggestYet, newRect){
biggestYet = Math.max (biggestYet, newRect)
return biggestYet;

}

// complete the definition using a call to reduce:
function compute() {
// return -23; // replace with code to return the area of
// the largest rectangle in rectangles
var biggestYet = 0;

for (x = 0 ; x < rectangles.length ; x++)
{
var rArea = area ( rectangles[x]);
console.log ( "area "+ x+ "," + rArea );

biggestYet = biggest (biggestYet, rArea);
console.log ( "biggest "+ x +","+ biggestYet);

}
return biggestYet;


}

</script>
</head>
<body>
<!-- the output is displayed using HTML -->
<p>The area of the largest rectangle is:
<!-- the ? will be replaced with the answer -->
<div id = "answer">?</div></p>
<br>
<!-- a button runs compute and puts the answer into the HTML -->
<button id = "computeButton"
onclick = "x = compute();
<!-- find the document element named 'answer' -->
where = document.getElementById('answer');
<!-- insert result x as text into the HTML -->
where.innerHTML = x.toString();">Run compute()
to compute the largest rectangle.</button>
</body>
</html>

最佳答案

您应该使用名为 reduce 的方法。您遇到的问题是因为 onclick 处理程序。你需要再调查一下。这是一个可能的解决方案:

function reduce(a, base, f) {
var result = base;
for (var i = 0; i < a.length; i++)
result = f(result, a[i]);
return result;
};

function area(rectangle) {
return rectangle.width * rectangle.height;
};

function biggest(result, rectangle) {
return Math.max(result, area(rectangle));
};

function compute(rectangles) {
var x = reduce(rectangles, 0, biggest);
var where = document.getElementById('answer');
where.innerHTML = x.toString();
};

var r1 = {width: 9, height: 34}; // area is 306
var r2 = {width: 10, height: 31}; // area is 310
var r3 = {width: 11, height: 28}; // area is 308
var r4 = {width: 12, height: 25}; // area is 300
var r5 = {width: 13, height: 24}; // area is 312
var r6 = {width: 14, height: 22}; // area is 308
var r7 = {width: 15, height: 20}; // 300
var r8 = {width: 16, height: 19}; // 304
var r9 = {width: 17, height: 18}; // 306
var r10 = {width: 18, height: 17}; // 306
var r11 = {width: 19, height: 16}; // 304
var r12 = {width: 20, height: 15}; // 300
var r13 = {width: 22, height: 14}; // 308
var r14 = {width: 24, height: 12}; // 288
var r15 = {width: 28, height: 11}; // 308

// form an array of all the rectangles
var rectangles = [r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15];

document.getElementById('computeButton').onclick = function() {
compute(rectangles);
};
        <!-- the output is displayed using HTML -->
<p>The area of the largest rectangle is:
<!-- the ? will be replaced with the answer -->
<div id = "answer">?</div></p>
<br>
<!-- a button runs compute and puts the answer into the HTML -->
<button id = "computeButton">Run compute()
to compute the largest rectangle.</button>

或者整个程序作为一个线程:

Math.max.apply(Math, rectangles.map(area));

关于javascript - 背包表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31041236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com