Blue Bridge Cup C language entry training sequence summation, Blue Bridge summation

Source: Internet
Author: User

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 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.