简体   繁体   English

仅添加奇数

[英]adding only odd numbers

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. 因此,例如,如果用户输入4,则我的程序应添加四个奇数。 1 + 3 + 5 + 7 = 16. 1 + 3 + 5 + 7 = 16。
The only tools I have available are for statement, if, if/else if,while loop and println. 我唯一可用的工具是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);
            }
        }
    }
}
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);
    }
}
sum = 0;

for (i = 1; i < n*2; i=i+2)
    sum = sum + i;

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

Maybe you know how to compute the sum of all numbers up to a given number n ? 也许您知道如何计算直到给定数字n的所有数字的总和? The formula is quite simple: (n * (n+1))/2 . 公式很简单: (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/2)* (n/2+1))/2) * 2 if N is even(ie the sum of all numbers up to n/2 times two that is because you have 2+4+6+8+...N = 2*(1+2+3+...n/2) ) (((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 if N is odd ((((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)

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. 它实际上是一行。

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);
int oddSum = 0;
for (int i = 0; i < n; i++){
  oddSum = oddSum + (i*2) + 1;
}

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

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