【问题标题】:Trouble finding sum of odd integers cubed [closed]难以找到奇数立方的总和[关闭]
【发布时间】:2019-03-21 11:34:03
【问题描述】:

尝试了几次,但仍然没有找到我的错误:这是我的程序。我需要从 1 和整数 x 中找到奇数,然后求它们的立方和。

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
  int x;
  int i = 1;
  int result;
  cout <<" Enter the value for n" << endl;
  cin >> x;

  while (i >x)
  if (i%2 == 0) {}

  else {

    result += pow(i,3);
    i++;
  }

  cout << "The sum of odd integers cubes from " << i << " to " << x << "= " << result << endl;



  return 0;

}

【问题讨论】:

  • 首先,你应该避开using namespace std,其次,看看while(i &gt; x),你好像漏掉了什么。
  • 也许要获得奇数,您可以考虑一个从 1 开始并以 2 递增的 for() 循环。例如:for (i=1; i&lt;x; i+=2) 那么您应该只有 i 的奇数。
  • 顺便说一句,pow 函数是浮点数并返回浮点数。乘法效率更高,例如i * i * i.
  • 您可以通过否定条件来跳过 if 中的空代码,例如if ((i%2) != 0) { result = i * i * i; ++i}

标签: c++ arrays loops for-loop while-loop


【解决方案1】:

您没有为 while 循环添加大括号。

while (i > x)
if (i%2 == 0) {}

需要:

 while (i > x){

 if (i % 2 == 0) {}

 }

另外,你在 if 语句中做了什么?你应该递减 x,看看每个数字是否都是奇数。

另外,您的程序提前结束,因为 i 是 1,如果用户输入大于 1 的数字,您的 while 循环甚至不会运行。您告诉 while 循环仅在 i 大于 x 时运行。尝试将其更改为小于:

来自:

 while (i > x){

到:

 while (i < x){

另外,你没有对 x 做任何事情。你想减少 x,而不是增加 i。虽然,我建议使用 do-while 循环。 (dowhile 循环在递增之前先进行一次迭代)

do{
if (x % 2 == 0) { // if the remainder of x/2 is 0, do this 
     x--;
     cout << "Equal: " << x << endl;
}
if(x % 2 != 0) { //if the remainder of x/2 is not 0, do this.  

   temp = pow(x,3); 
   //you don't want to take the power of the added sum, 
   //you were taking the power 3 of x which was being added to.
   //you want to add the sums of each power.  So here we have temp, a 
   //temporary variable to store your addends.
   result = result + temp;
   cout << "Not equal, temp:" << temp <<endl;
   cout << "Result: "<< result << endl;
   x--;  //you didn't have a decrement, you need to bring x eventually down to i if you want the loop to end, or even look through all of the numbers
   }
}
while (i < x);
//You have to have this semi colon here for the compiler to know its a do-while.
 cout << "The sum of odd integers cubes from " << i << " to " << userVar 
 << " = " << result << endl;

 return 0;
 }

注意:if-else 语句用于流控制,它就像真假,一个或另一个,以便您的数据将在某处流动。我使用了两个 if 语句,因为我想完全控制流程。

注意2:可以使用:

 using namespace std;

起初,但最终您想开始了解每个命令使用的库。当您开始进行更复杂的编程时,您会开始使用来自不同于标准库的命令。

【讨论】:

  • 请正确格式化您的答案。此外,您永远不应该鼓励使用using namespace std。最好现在改掉这个恶习,而不是以后再改。
  • 为什么他们被认为是一个坏习惯?我是编程新手,想知道
  • 非常感谢您的帮助
  • 他们被认为是一个坏习惯,因为以后在编程中你必须引用一个不在标准库中的不同库。该库中定义了许多基本功能/命令。我说一开始就可以使用它,因为对于初学者来说,双冒号可能会令人生畏,而且可能会令人困惑。不过,考虑到我确实生动地回答了您的问题,我不明白为什么值得投反对票。
  • >双冒号可能会让人望而生畏。我不同意这种说法。这是令人生畏的,因为它没有被正确地教授,并且在任何实践中都是可耻的。简单地将std:: 添加到标准库中的某些内容中,只要正确教授它就不会令人生畏。例如,您说,可以使用using namespace std; 而不解释它的作用、来源或使用原因。直到我上学的第二年,我才知道 using namespace std; 做了什么。坏习惯一旦开始就很难改掉。 @托马斯
【解决方案2】:

至少,您应该更改while中的比较
来自
while (i &gt; n)

while (i &lt;= n)

i 会大于输入的数字 n

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多