计算 $s=1+(1+2)+(1+2+3)+\cdots+(1+2+3+\cdots+n)$

 1 #include<stdio.h>
 2 //编写一个程序,计算 s=1+(1+2)+(1+2+3)+...+(1+2+3+...+n) 的值,要求n从键盘输入.
 3 main()
 4 {
 5 
 6   int i,j,n;
 7   int a,b;
 8   a=0;
 9   b=0;
10   printf("请输入n的值,它必须是个正整数:\n");
11   scanf("%d",&n);
12 
13   for (j=1;j<=n;j++)
14     {
15       for (i=1;i<=j;i++)
16     {
17       a=a+i;
18     }
19       b=b+a;
20       a=0;
21     }
22   printf("值为%d.\n",b);
23 }

 

posted @ 2013-09-14 13:44  叶卢庆  阅读(887)  评论(0)    收藏  举报