Blue Bridge Cup C language entry training sequence summation, Blue Bridge summation
The Problem description calculates the value of 1 + 2 + 3 +... + n. The input format includes an integer n. The output format outputs a row, including an integer, indicating the value of 1 + 2 + 3 +... + n. Sample input 4 sample output 10 sample input 100
Note: Some Questions will be provided with multiple sample input and output to help you better solve the problem.
Generally, all these samples must pass the test before submission, but this does not mean that the data of these samples is correct and your program is completely correct, potential errors may still cause low scores.
Sample output 5050 data scale and Convention 1 <= n <= 1,000,000,000.
Note: Pay attention to the data size here.
The direct idea of this question is to directly use a loop to accumulate data. However, when the data size is large, this "brute force" method will often lead to timeout. Now you need to think about other methods. You can try it. If 1000000000 is used as the input of your program, can your program run within the prescribed time limit.
Another important note of this question is that the answer size is not within the default INTEGER (int) range of your language. If you use an integer to save the result, it will lead to a result error.
If you use C ++ or C language and want to use printf to output the result, your format string should be written as % I64d to output an integer of the long type.
# Include <stdio. h> int main () {long a; scanf ("% I64d", & a); printf ("% I64d \ n", (a * (a + 1 )) /2); return 0 ;}