gpt4 book ai didi

在 C 中计算系列

转载 作者:太空宇宙 更新时间:2023-11-04 06:45:17 24 4
gpt4 key购买 nike

我为这个系列写了一个 C 程序。但是当我输入 n = -8 和 m = 2 时,结果为零。

为什么以及如何解决?

Series here

#include <stdio.h>
#include <math.h>

int main()
{
int n,m;
double sum = 0;
printf("Enter n:\n");
scanf_s("%d",&n);

printf("Enter m:\n");
scanf_s("%d", &m);

for (int i = -10; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
sum += (pow((i + j), 3) / pow(j,2) );
}
}
printf_s("%f",sum);
}

最佳答案

for (int j = 1; j <= n; j++)
{
sum += (pow((i + j), 3) / pow(j,2) );
}

当您尝试输入 n=-8 时,此循环将不起作用,因为 j=1并且条件设置为工作直到j<=n

关于在 C 中计算系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58732346/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com