简体   繁体   English

while循环测试用例错误

[英]while loop test case errors

The question as per the practice course is : 根据实践课程的问题是:

Write a JavaScript program to find the maximum integer n such that (1 + 2 + ... + n <= given integer ) is true. 编写一个JavaScript程序以找到最大整数n,使(1 + 2 + ... + n <=给定整数)为true。 For eg. 例如。 If a given integer is 10, value of maximum integer n is 4 so that 1+2+3+4 <= 10 is true. 如果给定的整数为10,则最大整数n的值为4,因此1 + 2 + 3 + 4 <= 10为真。 Your output code should be in the format console.log("Value of n is ", variableName) 您的输出代码应采用console.log(“ n的值是”,variableName)的格式。

My code is : 我的代码是:

 var num = prompt("Enter a number"); function test(x) { var sum = 1, n = 1, a = 0; while (sum <= x) { sum += n; n = n + 1; a += 1; } return a; } var output = test(num); console.log("Result is :", output); 

I'm getting the correct outputs as per the test cases I've entered(10-4,15-5,16-6,17-6) but the website says there is something wrong with the program. 根据输入的测试用例,我得到的输出正确(10-4、15-5、16-6、17-6),但是网站上说程序有问题。

What am i doing wrong? 我究竟做错了什么?

Better answer than looping: exploit maths. 比循环更好的答案:利用数学。 Starting with Triangular number formula : 三角数公式开始

1 + 2 + ... + n = n * (n + 1) / 2

Thus, for input x , you need to find n such that 因此,对于输入x ,您需要找到n使得

n * (n + 1) / 2 <= x

To solve this, we need to clean up the inequality, then use the quadratic equation formula : 为了解决这个问题,我们需要清理不等式,然后使用二次方程式

n^2 + n <= 2x
n^2 + n - 2x <= 0

n <= (-1 + sqrt(1 + 8x)) / 2

as the final solution. 作为最终解决方案。 eg for 例如

x = 10: n <= (-1 + sqrt(81)) / 2; n <= 4
x = 16: n <= (-1 + sqrt(128)) / 2; n <= 5.156854249492381

Round the upper limit down, and you have the largest allowed integer. 向下舍入上限,您将拥有最大的允许整数。 Translated into JavaScript: 翻译成JavaScript:

 function test(x) { return Math.floor((Math.sqrt(8 * x + 1) - 1) / 2); } var num = prompt("Enter a number"); console.log("Result is :", test(num)); 

Consider if the passed value is 11. Then, the maximum integer n should be 4, because 1+2+3+4 < 11 is true, while 1+2+3+4+5 < 11 is false. 考虑传递的值是否为11。然后,最大整数n应该为4,因为1+2+3+4 < 11为真,而1+2+3+4+5 < 11为假。 Your current code outputs 5 for an input of 11, though, which is incorrect; 您的当前代码在输入11时输出5,但这是不正确的; your while loop is sometimes overshooting sum . 你的while循环有时超过了sum

You also need to initialize sum to start at 0, not at 1. 您还需要初始化sum以从0开始,而不是从1开始。

Subtract one from a before returning it: a减去一个,然后返回:

 function test(x) { var sum = 0, n = 1, a = 0; while (sum <= x) { sum += n; n = n + 1; a += 1; console.log(a, sum); } return a - 1; } console.log(test(10)); console.log(test(11)); var num = prompt("Enter a number"); var output = test(num); console.log("Result is :", output); 

I think this will work for you: 我认为这将为您工作:

 var num = prompt("Enter a number"); function test(x) { var sum = 1, n = 0; while ((sum+n) <= x) { n = n + 1; sum += n; } return n; } var output = test(num); console.log("Result is :", output); 

The code below should work for you. 下面的代码应该为您工作。 Basically, what I did was that if the input is 10, and your sum is 9, it will still go into the while loop. 基本上,我所做的是,如果输入为10,而您的总和为9,则仍然会进入while循环。 Then it will add n again and now your number is greater than your input (which is 10), but you still return it. 然后它将再次加n,现在您的数字大于输入的数字(是10),但是您仍然返回它。 Here what I did is that at the end of the while loop, if your sum is greater than your input, subtract one from a. 我在这里所做的是,在while循环结束时,如果您的总和大于输入,请从a中减去1。 That way it will still execute, but it will fix the problem. 这样,它仍然可以执行,但可以解决问题。

Also another error I noticed was that sum started at 1, and n started at 1. You wanted 1+2+3+...+n, however using your previous method, you got 1+1+2+3+...+n. 我还注意到的另一个错误是,总和从1开始,n从1开始。您想要1 + 2 + 3 + ... + n,但是使用先前的方法,您得到了1 + 1 + 2 + 3 +。 。+ N。

 var num = prompt("Enter a number"); function test(x) { var sum = 0, n = 1, tempSum = 1, a = 0; while (sum <= x) { sum += n; n++; a++; if (sum > x) { a--; } } return a; } var output = test(num); console.log("Result is :", output); 

Your order of operation is a little funky; 您的操作顺序有点时髦; all you have to do is add the incrementor. 您要做的就是添加增量器。 The while false case will make sure the sum only passes over the number once. while假的情况将确保总和仅超过数字一次。 So when you return, reduce the number by one: 因此,当您返回时,将数字减少一:

 var num = prompt("Enter a number"); var output = test(num); console.log("Result is :", output); function test(num){ let sum = 0 let inc = 0 while(sum<=num) sum+=++inc return --inc; } 

This is a reduced version of your code, basically we increment first the number to add ( n ) in each iteration, and then we add it to the variable holding the sum . 这是代码的简化版本,基本上,我们首先在每次迭代中增加要加( n )的数字,然后将其添加到保存sum的变量中。 When the loop conditions evaluates to false you need to decrement one to n to get your value: 当循环条件评估为false您需要将n减1以获取您的值:

 var num = prompt("Enter a number"); function test(x) { var sum = 0, n = 0; while (sum <= x) { sum += (++n); } return --n; } var output = test(num); console.log("Result is :", output); 

Try below function to find max Number 尝试以下功能找到最大数量

function maxNumber(a){
 var i=1,sum=0,maxNumber=0;
 while(sum<=a) { 
     sum=sum+i; 
     if(sum<=a) 
      { 
        maxNumber=i;
      }
      i+=1;
   } 
   return maxNumber;
} 

doubled checked condition sum<=a to preserve the previous loop value and if condition not satisfied that means current loop value is not useful so returned preserved value of previous loop 将检查的条件sum<=a加倍以保留先前的循环值,如果不满足条件则意味着当前循环值无用,因此返回先前循环的保留值

Output tested : 输出测试

在此处输入图片说明

Below will help you get the job done. 下面将帮助您完成工作。

 var num = prompt("Enter a number"); function findMaxNumber(num){ var sum = 0; var counter = 0; while(sum < num){ if(sum + counter > num){ break; // Exit loop } sum = sum + counter; counter++; } return --counter; // Loop will cause this to be 1 higher than the max int. } console.log('Result is: ' + findMaxNumber(num)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM