Challenges of CSDN programming-Gaussian Formula

Source: Internet
Author: User

Gaussian formula details:

Gauss invented the Sum Formula of the arithmetic difference series in elementary school: 1 + 2 +... + 100 = 5050. Now the question is to give you a positive integer n. How many continuous positive integers can you represent? (Itself ).

Input Format:

Multiple groups of data, one row of each group of data, a positive integer n. 0 <n <2000000000

Output Format:

A row of data in each group contains a positive integer, indicating the result.



Q & A description:

Input example

5

120

Output example:

2

4

Explanation:

5 = 2 + 3 = 5

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



Draft Code:

/* 3:3, 1 + 2 = 34: 4, 5:5, 2 + 3 = 56: 6, 1 + 2 + 3 = 6 7: 7, 3 + 4 =, 4 + 5 = 9, 2 + 3 + 4 = 910: 10, 1 + 2 + 3 + 4 = 10 analysis: input n to set s, x (x continuous numbers starting from s add n, for example: 10 = 1 + 2 + 3 + 4 n: 10 s: 1 x = 4) so there is a summation formula: (s + x-1) ----------- * x = n2 according to the summation formula brute force to solve */# include "stdio. h "# include" math. h "int main () {int count; long x, n; while (scanf (" % I64d ", & n )! = EOF) {count = 1; for (x = 2; x <n; x ++) {double s = (n + (x-x)/2.0) /x; if (s> = 1) {if (floor (s + 0.5) = s) {// judge s as an integer printf ("% d> x: % I64d s: % d \ n ", count + 1, x, (int) s); count ++;} else {printf (" % d, % lf not fully divided! \ N ", x, s) ;}} else {printf (" a maximum of % d is allowed! \ N ", x); break;} printf (" result: % d \ n ", count); break;} return 0 ;}


AC post 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(floor(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: When the data type in the program is int, it may overflow during the calculation of relatively large data.

CSDN challenge programming exchange group: 372863405



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.