简体   繁体   English

C ++编程帮助

[英]C++ Programming help

You create a program that displays the sum of even integers between and including two numbers entered by the user .. 您创建一个程序来显示用户输入的两个数字之间(包括两个数字)的偶数整数之和。

ex) 2 and 7 = the sum of 12 (2+4+6) 例如)2和7 = 12之和(2 + 4 + 6)

this is what i have so far! 这就是我到目前为止! butt if u can just put me in the right direction that would be helpful 如果您能使我朝正确的方向前进,那将会很有帮助

//Advanced30.cpp - displays the sum of the even integers between and 
//including two numbers entered by the user
//Created/revised by <your name> on <current date>

#include <iostream>
using namespace std;

int main()
{
    // declare variables
    int num1 = 0;
    int num2 = 0;
    int sum= 0;

    cout << "Enter the First Number:" << endl;
    cin >> num1;
    cout << "Enter the Second Number:" << endl;
    cin >> num2;

    if ( num1 > num2)
    { 
    cout << "Invalid entry. Final number must be less than the first number. Please try again." << endl;
    }
    for ( int sum = (((num1 + 1)/2)*2); num1 <= (((num2 + 1)/2)*2) ; sum = 2 + (((num1 + 1)/2)*2) )





    return 0;
}   //end of main function

In your for loop it should be like this. 在您的for循环中,应该像这样。

double sum = 0.0;
for(i = num1; i <= num2; i++){
if(i % 2 == 0){  // Is our number even
   sum += i;
  }
}

That's it and it print out sum. 就是这样,它打印出总和。

I would simplify your for loop 我会简化你的for循环

for(int i = num1; i <= num2; i++) {
    if(i % 2 == 0) sum += i;
}

This will look at twice as many numbers, but honestly that's not all that much more expensive. 这个数字将是原来的两倍,但是说实话,这并没有那么昂贵。

You could also do it in O(1) time by taking advantage of the fact that the sum 1..n == n*(n+1) 您还可以利用总和1..n == n *(n + 1)的事实在O(1)时间内完成此操作

Here's a very simple example in Java, translating it to C++ won't be too difficult I hope :) no C++ compiler on this machine :-X 这是Java中的一个非常简单的示例,将它翻译成C ++不会太困难,我希望:) 这台机器上没有C ++编译器:-X

import java.util.*;
class DoubleSum {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();

        int low = (num1 - 1)/ 2;
        int high = num2 / 2;

        int sumLow = (low*(low + 1));
        int sumHigh = (high*(high + 1));

        int sum = sumHigh - sumLow;

        System.out.println(sum);
    }
}

You are using the same variable to control the for loop and to the sum, this won't work. 您正在使用相同的变量来控制for循环和求和,这将不起作用。 Try this: 尝试这个:

int even1 = num1 % 2 == 0 ? num1 : num1+1;
int even2 = num2 % 2 == 0 ? num2 : num2-1;
for (int i = even1; i <= even2; i += 2) sum += i;

Note that you don't really need a for loop: 请注意,您实际上并不需要for循环:

int even1 = num1 % 2 == 0 ? num1 : num1+1;
int even2 = num2 % 2 == 0 ? num2 : num2-1;

// how many numbers you will sum (remember they are even, so we need to divide by 2)
int count = 1 + (even2 - even1)/2;

sum = (even1 + even2) * (count/2);
if (count % 2 == 1) sum += (even1 + even2)/2;
for(int i = num1; i <= num2; i++)
{
  if(!(i & 1))  
     sum += i;
}

Your code would end up in an infinite loop. 您的代码将最终陷入无限循环。

Look at the for() loop. 查看for()循环。 You have the condition 你有条件

num1 <= (((num2 + 1)/2)*2)

to determine whether your loop terminates. 确定循环是否终止。 However, since num1 itself is never incremented, and num1 < num2 is guaranteed, this condition will always be true - which means your for loop would never end. 但是,由于num1本身从未增加,并且num1 <num2得到保证,因此此条件将始终为true-这意味着您的for循环将永远不会结束。 I would also suggest using a separate looping variable. 我也建议使用一个单独的循环变量。

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

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