求助 c语言 计算S = 1+(1+2)+(1+2+3)+…+(1+2+…+N)。已知N,要求写程序求出S。

#include <stdio.h>
int main()
{
int i,n,s=0,t=0,a,j;
scanf("%d",&a);
for(j=1;j<a+1;j++)
{
scanf("%d",&n);
for(i=1;i<=n;i++)

{
t=t+i;
s=s+t;
}
printf("%d\n",s);}

return 0;
}

输入
2
1
1
输出
1
2
怎么改?

第1个回答  2016-11-11
#include <stdio.h>
int main()
{
int i,n,s=0,t=0,a,j;
scanf("%d",&a);
for(j=1;j<a+1;j++)
{ s=0;
scanf("%d",&n);
for(i=1;i<=n;i++)

{
t=t+i;
s=s+t;
}
printf("%d\n",s);}

return 0;
}
记的s每次清0
望采纳,谢谢追问

不好意思是我没表达清楚,我那个输出是错的
输入
2
1
200
输出
1
1353400

能再改改吗 大神

追答

我已经改过了啊,见我上面发的程序,清0就可以了

本回答被网友采纳
第2个回答  2018-02-27
你的问题描述的不是很清楚,如果只是简单的输入一个 N ,输出对应的 S 的话,程序如下:
#include <stdio.h>
int main()
{
int i,n,s=0,t,a,j;
scanf("%d",&n);
for(j=1;j<n+1;j++)
{
t=0;
for(i=1;i<=j;i++)
{
t=t+i;
}
s=s+t;
}
printf("%d",s);
return 0;
}
运行后,当你输入 2 时,s=1+1+2=4,当你输入 3 时,s=1+1+2+1+2+3=11
第3个回答  2016-11-11
#include <stdio.h>
int main(){
int i,n,s=0,t=0,a,j;

scanf("%d",&a);
for(j=1;j<=a;j++){
for(i=1;i<=j;i++){
t=t+i;
}
s=s+t;
t=0;
}
printf("%d\n",s);
return 0;
}
还有,写程序要有一个好的格式!
相似回答