java 只添加奇数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13395898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
adding only odd numbers
提问by Jessica M.
So the question I'm trying to solve the user is supposed to enter any positive number. Then I'm trying to write a program that adds only the odd numbers up to the number the user enters and displays the total. So for example if the user enters 4 my program should add four odd numbers. 1 + 3 + 5 + 7 = 16.
The only tools I have available are for statement, if, if/else if,while loop and println.
所以我试图解决用户的问题应该输入任何正数。然后我试图编写一个程序,只将奇数与用户输入的数字相加并显示总数。例如,如果用户输入 4,我的程序应该添加四个奇数。1 + 3 + 5 + 7 = 16.
我唯一可用的工具是 for 语句、if、if/else if、while 循环和 println。
I can only figure out how to print out the odd numbers. I know I want to create a variable named total to store the value of adding up all the odd numbers but I don't know how that fits into the program.
我只能弄清楚如何打印出奇数。我知道我想创建一个名为 total 的变量来存储所有奇数相加的值,但我不知道它如何适合程序。
import acm.program.*;
public class AddingOddNumbers extends ConsoleProgram {
public void run() {
int n = readInt("enter a positive nunber: ");
int total = 0;
for (int i = 0; i < n; i++) {
if (n == 1) {
println(1);
} else {
println((i * 2) + 1);
}
}
}
}
采纳答案by Alexandre Lavoie
import acm.program.*;
public class AddingOddNumbers extends ConsoleProgram {
public void run() {
int n = readInt("enter a positive nunber: ");
int total = 0;
for (int i = 0; i < n; i++) {
if (n == 1) {
println(1);
} else {
println((i * 2) + 1);
total += (i * 2) + 1;
}
}
println("total : " + total);
}
}
回答by AurA
sum = 0;
for (i = 1; i < n*2; i=i+2)
sum = sum + i;
回答by P.P
This will give you the odd number sum.
这将为您提供奇数总和。
if (n>0)
{
total=0;
for (int i = 1; i < n; i ++){
if (i%2 == 1)
total+=i;
}
}
If you want to inclusive of n, then change the condition to i<=n
.
如果要包含 n,则将条件更改为i<=n
。
回答by Ivaylo Strandjev
Maybe you know how to compute the sum of all numbers up to a given number n
? The formula is quite simple: (n * (n+1))/2
. Now getting the sum of only the odd numbers is a bit trickier but - no worries you can make use only of the previous formula for that. First notice that the sum of all even numbers up to a given number n is:
也许你知道如何计算所有数字的总和直到给定的数字n
?公式很简单:(n * (n+1))/2
. 现在只计算奇数的总和有点棘手,但是 - 不用担心您只能使用前面的公式。首先注意直到给定数字 n 的所有偶数的总和是:
(((n/2)* (n/2+1))/2) * 2
if N is even(i.e. the sum of all numbers up to n/2 times two that is because you have2+4+6+8+...N = 2*(1+2+3+...n/2)
)((((n-1)/2)* ((n-1)/2+1))/2) * 2
if N is odd
(((n/2)* (n/2+1))/2) * 2
如果 N 是偶数(即所有数字的总和最多为 n/2 乘以 2 那是因为你有2+4+6+8+...N = 2*(1+2+3+...n/2)
)((((n-1)/2)* ((n-1)/2+1))/2) * 2
如果 N 是奇数
In fact if you have integer division the formula is always: (((n/2)* (n/2+1))/2) * 2 = (n/2)* (n/2+1)
事实上,如果你有整数除法,公式总是: (((n/2)* (n/2+1))/2) * 2 = (n/2)* (n/2+1)
So to compute the sum of all the odd numbers up to n you simply subtract the sum of the even numbers from the sum of the all numbers:
因此,要计算直到 n 的所有奇数的总和,您只需从所有数字的总和中减去偶数的总和:
(n * (n+1))/2 - (n/2)*(n/2+1)
(n * (n+1))/2 - (n/2)*(n/2+1)
In fact if you observe closely you will notice that the sum 1+3+...(2*n-1)
always equals to n^2
.
事实上,如果你仔细观察,你会发现总和1+3+...(2*n-1)
总是等于n^2
。
This answer should help you solve your problem in all languages and I am leaving the code to you. It is literally one line.
这个答案应该可以帮助您解决所有语言的问题,我将代码留给您。它实际上是一行。
回答by Peter Lawrey
I would use a loop for the odd numbers as well.
我也会对奇数使用循环。
for (int i = 0, j = 1; i < n; i++, j += 2) {
println(j);
total += j;
}
println(total);
回答by The Cat
int oddSum = 0;
for (int i = 0; i < n; i++){
oddSum = oddSum + (i*2) + 1;
}