简体   繁体   中英

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

计算公式

As @Yksisarvinen said,

Hint

Multiplication is inside summation in the formula.

Hint 2

You can use 2 for loop inside each others

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 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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