我必须写一个方法int SumBeyond(int k)
来找到最小的n,使得小于n的自然数之和超过k。但是,当我尝试测试该方法时,它不会返回任何值。
public static int sumBeyond(int k){
int i=1;
int sum=0;
while(i<k){
sum=sum+i;
i=i+1;
}
return sum;
}
我尝试这样调用函数
,在main方法中:
sumBeyond(100);
如上所述,问题不是您的方法没有返回某些内容,而是您没有对返回的内容执行任何操作。此外,如上所述,您专注于寻找总和,但这实际上不是问题所问的。我很赞同您在这里使用while循环,因为它可能根本不会执行,而且您也不知道它会运行多少次。所以我重写了它来检查正确的东西,并改编了deHaar的main来练习它。这让我能够亲自检查答案,因为有些情况下,平等和需要“数字小于”而不是“数字小于或等于”是微妙的。我的数学老师非常喜欢Joop Eggen的二次公式法;这只是很难做到正确(事实上,如果我要做的话,我最终会测试它是否与我这里的一致)。
public class ShadesOfLittleGauss {
public static int sumBeyond(int k) {
int i = 1; //have summed (trivially) all natural numbers less than 1 so far
int sum = 0;
while (sum <= k) { //needs to exceed, so <=
sum = sum + i;
i = i + 1;
}
return i;
}
public static void main(String[] args) {
for (int k = -1; k < 10; k++) {
System.out.println("The least number " +
"such that the sum of the natural numbers smaller than n exceeds " +
k + " is " + sumBeyond(k));
}
}
}
输出(您可以手动检查是否正确,如所述):
The least number such that the sum of the natural numbers smaller than n exceeds -1 is 1
The least number such that the sum of the natural numbers smaller than n exceeds 0 is 2
The least number such that the sum of the natural numbers smaller than n exceeds 1 is 3
The least number such that the sum of the natural numbers smaller than n exceeds 2 is 3
The least number such that the sum of the natural numbers smaller than n exceeds 3 is 4
The least number such that the sum of the natural numbers smaller than n exceeds 4 is 4
The least number such that the sum of the natural numbers smaller than n exceeds 5 is 4
The least number such that the sum of the natural numbers smaller than n exceeds 6 is 5
The least number such that the sum of the natural numbers smaller than n exceeds 7 is 5
The least number such that the sum of the natural numbers smaller than n exceeds 8 is 5
The least number such that the sum of the natural numbers smaller than n exceeds 9 is 5
更新:我确实解决了二次型,并提出了以下与更简单的方法一致的方法。
public static int sumBeyond2(int k) {
if (k < 0) { //do not take squareroots of negatives
return 1;
}
double x = -1.0 / 2 + Math.sqrt(1 + 8 * k) / 2; //from solving quadratic inequality n(n+1)/2.0 > k
if (Math.abs(Math.round(x) - x) < 0.0000001) { //special case, it is an integer, so ceil won't reliably add 1
return 1 + 1 + (int) Math.round(x);
}
return 1 + (int) Math.ceil(x); //adding 1 because of wording, integers less than, ceil because needs to exceed k
}
如果我答对了你的问题,你想找到最小的n
,使得小于n的自然数之和超过k
,因此,你不应该返回总和本身,因为它不是n
,但需要计算才能找到最小的n
。
您可以通过以下方式执行此操作:
public static int sumBeyond(int k) {
int n = 0;
int sum = 0;
for (int i = 0; i < k; i++) {
// provide an intermediate sum (the one before this step) for logging purpose
int intermediateSum = sum;
// sum up
sum += i;
// set the return value to the current "natural number"
n = i;
// print some detailed debug log
System.out.println("sum:\t" + sum +
" (" + intermediateSum + " + " + i + ")\t——>\tn = " + n);
// exit the loop if the sum is greater than k
if (sum >= k) {
break;
}
}
return n + 1;
}
像这样在main
中调用它
public static void main(String[] args) {
int k = 100;
System.out.println("The least n" +
+ "such that the sum of the natural numbers smaller than n exceeds "
+ k + " is " + sumBeyond(k));
}
将打印
sum: 0 (0 + 0) ——> n = 0
sum: 1 (0 + 1) ——> n = 1
sum: 3 (1 + 2) ——> n = 2
sum: 6 (3 + 3) ——> n = 3
sum: 10 (6 + 4) ——> n = 4
sum: 15 (10 + 5) ——> n = 5
sum: 21 (15 + 6) ——> n = 6
sum: 28 (21 + 7) ——> n = 7
sum: 36 (28 + 8) ——> n = 8
sum: 45 (36 + 9) ——> n = 9
sum: 55 (45 + 10) ——> n = 10
sum: 66 (55 + 11) ——> n = 11
sum: 78 (66 + 12) ——> n = 12
sum: 91 (78 + 13) ——> n = 13
sum: 105 (91 + 14) ——> n = 14
The least n such that the sum of the natural numbers smaller than n exceeds 100 is 15
我真的希望我做对了,还是不确定
哦,如果0
不是一个自然数,那么从1
开始迭代。
int sum100 = sumBeyond(100);
System.out.println("Sum is " + sum100);
然后小改进:
public static int sumBeyond(int k) {
int i = 1;
int sum = 0;
while (i < k) {
sum += i;
++i;
}
return sum;
}
public static int sumBeyond(int k) {
int sum = 0;
for (int i = 1; i < k; ++i) {
sum += i;
}
return sum;
}
public static int sumBeyond(int k) {
// return (k - 1) * (1 + k - 1) / 2;
return (k - 1) * k / 2;
}
为了解决问题指出:
所以我们得到:
x² - x - 2k'
------------ >= 0
2
=0
的解决方案:
a = 1/2
b = -1/2
c = -2k'
_________
-b +/- V b² - 4ac
x = ------------------
2a
x = 1/2 +/- sqrt(1/4 + 4k'/4) =
= 1/2 +/- 1/2 . sqrt(1 + 4k')
正x
x = 1/2 + 1/2 . sqrt(4k' + 1)
public static int sumBeyond(int k) {
double x = (Math.sqrt(4 * (k-1) + 1) + 1) / 2;
return (int) Math.ceil(x);
}
解决方案应该作为注释给出数学。
问题内容: 我在React中有一个要在index.js中导入的组件,但是它给出了这个错误: 渲染未返回任何内容。这通常意味着缺少return语句。或者,不渲染任何内容,则返回null index.js: 零件: 问题答案: 我在 render() 方法中遇到了同样的问题。当您从 render() 返回时出现问题: 即如果您在新行中开始括号 尝试使用: 这样可以解决错误
问题内容: JDK中是否有一个标准的功能接口,该接口什么都不做,什么也不返回?我找不到一个。类似于以下内容: 问题答案: 那么Runnable呢:
为什么我不能在不使用return语句的情况下运行此代码段?
我有一个像这样的mysql表 但是我无法用这个函数检索“成人”和“儿童”的值 错误日志显示变量被正确地传递给函数: 为什么此函数返回?
如何获取java主目录? 这样做的时候 一无所获
我的问题是: 接下来我用 它返回[],即不返回任何内容 但是如果我设置cursor=conn.cursor(),同样的查询会返回一些行!怎么了?为什么我不能使用游标?