C语言 编写程序来执行 sum = 1+ (1+2) + (1+2+3) + ... + (1+2...+n)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3249611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
write program to perform sum = 1+ (1+2) + (1+2+3) + ... + (1+2...+n)
提问by user391967
I can't get the codes right. Can somebody help?
我无法正确获取代码。有人可以帮忙吗?
#include<stdio.h>
int main()
{
int n, sum,i,j;
printf("Please enter an integer, n = ");
scanf("%d", &n);
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
sum = sum + n;
printf("sum = %d", sum);
return 0;
}
回答by IVlad
- You are not initialising
sum. Initialise it with0. - You shouldn't be adding
nat each step, butj.
- 你没有初始化
sum。用 初始化它0。 - 您不应该
n在每一步都添加,而是j.
Of course, this is to fix your current code. There are better approaches to solving the problem, which others have already mentioned.
当然,这是为了修复您当前的代码。有更好的方法来解决这个问题,其他人已经提到过。
Edit:
编辑:
Just for fun, here's a formula that allows you to solve the problem in O(1):
只是为了好玩,这里有一个公式可以让您解决问题O(1):
Your sum is equal to n*(n + 1)*(2*n + 1) / 12 + n*(n + 1) / 4.
你的总和等于n*(n + 1)*(2*n + 1) / 12 + n*(n + 1) / 4。
This is obtained by writing it as a sum and using the fact that the sum of the first nconsecutive squares is n(n + 1)(2n + 1) / 6and the sum of the first npositive ints is n(n + 1)/2. +1 if you can find a nicer form of the formula.
这是通过将其写为总和并使用第一个n连续平方n(n + 1)(2n + 1) / 6的总和为 和第一个n正整数的总和为 的事实来获得的n(n + 1)/2。+1 如果您能找到更好的公式形式。
回答by Tim Pietzcker
No need for recursion, just look at the math:
不需要递归,只看数学:
1 + (1+2) + (1+2+3) + ... + (1+2+3+...+n)
is equal to
等于
1*n + 2*(n-1) + 3*(n-2) + ... + n
回答by zvone
Not what you expected, but this isthe best solution ;)
不是您所期望的,但这是最好的解决方案;)
int calculate (int n) {
return (2*n + 3*n*n + n*n*n) / 6;
}
回答by Asker
Doing it iteratively, like you tried:
反复进行,就像您尝试过的那样:
#include <stdio.h>
int main() {
int i, t, n, sum;
printf("Please enter an integer, n = ");
scanf("%d", &n);
t = sum = 0;
for (i = 1; i <= n; ++i) {
t += i;
sum += t;
}
printf("sum = %d\n", sum);
return 0;
}
But there's a closed-form formula as IVlad suggested.
但正如 IVlad 所建议的那样,有一个封闭式公式。
回答by Steven Sudit
Think this through. You have one sum you're accumulating, and you have a series of values. Each value can be generated from the previous one by adding the index. So why do you have nested loops?
仔细想想。您有一个正在累积的总和,并且您有一系列值。每个值都可以通过添加索引从前一个值生成。那么为什么要嵌套循环呢?
回答by Chris Dodd
You never initialize sum, so you're adding everything to a random garbage value. Stick sum = 0;before your loops
您永远不会 initialize sum,因此您将所有内容添加到随机垃圾值中。坚持sum = 0;在你的循环之前
回答by Greg Domjan
Start with the math, see if you can find some pattern.
从数学开始,看看你能不能找到一些规律。
if n = 0 , res = 0?
if n = 1 , res = 1
if n = 2 , res = 1 + (1+2)
if n = 3 , res = 1 + (1+2) + (1+2+3)
for each n, res is ??
对于每个 n, res 是 ??
回答by JonH
Try this:
尝试这个:
int main(void)
{
int total=1;
int sumtotal = 0;
int n=5;
for(int i=1; i<=n; i++)
{
total+=i;
sumtotal+=total;
}
//sumtotal should give you 1+(1+2)+(1+2+3)
return 0;
}
回答by zerogiant
Try:
尝试:
int main(void)
{
int n, sum;
printf("\nPlease enter a postive integer value");
scanf("%d", &n);
sum = n * ( n + 1 )/ 2;
printf("\n%d", sum);
return 0;
}
回答by Andy Reid
n * (n -(n - 1))
n * (n -(n - 1))
int n, sum
for n = 0; n <= 3; n ++ {
sum += n * (n -(n - 1))
}
works for factorials too (change operator and set sum to 1):
也适用于阶乘(更改运算符并将总和设置为 1):
int n, sum
sum = 1
for n = 0; n <= 3: n++{
sum *= n * (n -(n -1))
}

