Blue Bridge Cup-introductory training sequence summation

Source: Internet
Author: User

Get started training sequence summationTime limit: 1.0s memory limit: 256.0MBThe problem description asks for the value of 1+2+3+...+n. Input format input includes an integer n. The output format outputs a row, including an integer that represents the value of the 1+2+3+...+n. Sample Input 4 Example output 10 sample input 100

Note: There are a few questions that will give you a sample input and output to help you do a better job.

Typically all of these samples require a test pass before committing, but this does not mean that the sample data are correct and your program is completely correct, and potential errors may still result in a lower score.

Sample output 5050 data size and convention 1 <= N <= 1,000,000,000.

Description: Please note the size of the data here.

The direct idea is to use a loop to accumulate, however, when the data size is large, this "brute force" approach often leads to timeouts. At this point you need to think about other ways. You can try it, if you use 1000000000 as input to your program, your program will not be able to run it within the specified time limit.

Another important point to note is that the size of the answer is not in the range of the default int (int) of your language, and if you use an integer to save the result, the result is wrong.

If you are using C + + or C and are ready to use printf to output the results, your format string should be written as%i64d to output a long long integer.

 #include <iostream> #include  <cstdio> using  namespace   Std;  int      main () {__int64 sum,n;  while  (~scanf ( " %i 64d  , &n)) {Sum  = ((1  +n) *n)/2  ; printf (  %i64d\n   " ,sum);  return  0  ;}

Blue Bridge Cup-introductory training sequence summation

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.