简体   繁体   English

c ++解决数学公式的问题

[英]c++ problem with solving mathematic formula

im struggle to solve this math formula and i don't see where i made mistake.我很难解决这个数学公式,但我不知道我在哪里犯了错误。 Little hint would be welcome.欢迎小提示。

using namespace std;
double sum, quo;
int n, i;
sum = 0;
quo = 1;
for (n = 1; n <= 5; n++) {

    sum = sum + quo;
}
for (i = 1; i <= 6; i++) {

    quo = quo * (n + i);
}
sum = sum + quo;

 cout << (sum);}

Answer should be 569520, but in my code is 665285答案应该是 569520,但在我的代码中是 665285

计算公式

As @Yksisarvinen said,正如@Yksisarvinen 所说,

Hint暗示

Multiplication is inside summation in the formula.乘法是公式中的求和。

Hint 2提示 2

You can use 2 for loop inside each others您可以在彼此内部使用 2 个 for 循环

Stop here and try it yourself then come back to see the answer.停在这里,自己尝试一下,然后回来查看答案。

the answer :答案 :

#include <iostream>
#include <windows.h>
using namespace std;
int main() {
int sum, quo;
int n, i;
 sum = 0;
 quo = 1;
for (n = 1; n <= 5; n++) {
    for (i = 1; i <= 6; i++) {
            quo *= (n + i);

    }
    sum+=quo;
    quo =1;
}
 cout << (sum);
}

It's been a while since I've done this sort of maths, but I think your nesting is incorrect.我已经有一段时间没有做这种数学了,但我认为你的嵌套是不正确的。

What I think the formula is saying is:我认为公式的意思是:

((1 + 1) * (1 + 2) * (1 + 3) ...)
+
((2 + 1) * (2 + 2) * (2 + 3) ...)
+
...

However, you're summing loop only apply to i=1.但是,您求和循环仅适用于 i=1。 I think this is just an incorrectly placed brace.我认为这只是一个错误放置的支架。

for (n = 1; n <= 5; n++) {
    //The n loop should encompass the whole of the i loop
    //And you should only update sum at the end
    double quo = 1;
    for (i = 1; i <= 6; i++) {
        quo = quo * (n + i);
    }
    sum = sum + quo;
}

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

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