1010

题目:

Sum Problem

题解:

可以用循环不断做加法,也可以用数学方法高斯求和(1+n)*n/2.

代码:

#include <stdio.h>
main() 
{
    int sum, n, i;
    while (scanf("%d", &n) != EOF) {
        sum = 0;
        for (i = 0; i <= n; i++) {
            sum += i;
        }
        printf("%d\n\n", sum);//按题目所要求做输出
    }
}

相关文章:

  • 2022-01-19
  • 2021-12-06
  • 2021-09-03
  • 2021-08-21
  • 2021-07-07
  • 2021-06-25
  • 2021-05-29
  • 2021-05-16
猜你喜欢
  • 2021-09-08
  • 2021-09-28
  • 2021-06-07
  • 2021-08-26
  • 2021-06-06
  • 2021-12-12
  • 2021-04-22
相关资源
相似解决方案