CSDN Programming Challenge--"Gauss formula"

Source: Internet
Author: User

Gaussian Formula topic Details:

In elementary school, Gauss invented the arithmetic progression summation formula: 1+2+. +100=5050. Now the problem is to give you a positive integer n, and ask you how many consecutive positive integers can he represent? (self-calculation).

Input format:

Multiple sets of data, one row per set of data, and a positive integer n.

0<n<2000000000

Output format:

A row of data for each group, including a positive integer, representing the result.



Answer instructions:

Input example

5

120

Output Example:

2

4

Explain:

5=2+3=5

120=1+2+...+15=22+23+24+25+26=39+40+41=120



First Draft Code:

/*3:3, 1+2=34:      4, 5:5, 2+3=56:6, 1+2+3=6 7:     7, 3+4=7 8:8, 9:9, 4+5=9, 2+3+4=910:    10, 1+2+3+4=10 Analysis: input n set s,x ( X starts from S continuous number addition equals N, for example: 10=1+2+3+4 in N:10 s:1 x=4) Then there is a summation formula:  


Post-AC code:

#include "stdio.h" #include "math.h" int main () {int count;long long x,n;while (scanf ("%i64d", &n)!=eof) {double s=n; for (count=0,x=2;s>=1;x++) {if (s+0.5) ==s) {count++;//printf ("%d >  x:%d  s:%d\n", count,x-1, (int) s);} S= (Double) n/x+ (1-x)/2.0;} printf ("%d\n", count);//break;} return 0;}

Note: Data in a program with a large data type of int control may overflow during calculation

CSDN Challenge Programming Exchange Group:372863405



CSDN Programming Challenge--"Gauss formula"

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.