ACM HDU 1001
Problem Description
Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
Sum Problem
Input
The input will consist of a series of integers n, one integer per line.
Output
For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.
Sample Input
1
100
Sample Output
1 5050
解题思路:很简单,求累加和。然而我提交了9次才AC了两次。
代码如下
第一次AC
#include <stdio.h> int sso(int n); int main(void) { int i; while (scanf_s("%d", &i) != EOF) { printf("%d\n",sso(i)); putchar('\n'); } return 0; } int sso(int n) { int i, sum=0; for (i = 0; i <= n; i++) sum += i; return sum; }
第二次AC
#include <stdio.h> int main(void) { int a = 0, b, sum = 0; while (scanf_s("%d", &b) != EOF) { sum = 0, a = 0; do { a = a + 1; sum = a + sum; } while (a<b); printf("%d\n", sum); printf("\n"); } return 0; }