Sum of Consecutive Integers (Mathematics) of 51nod 1138, 51nod1138

Source: Internet
Author: User

Sum of Consecutive Integers (Mathematics) of 51nod 1138, 51nod1138

Description:

Http://www.51nod.com/onlineJudge/questionCode.html! ProblemId = 1138

Returns a positive integer N, which is written into several continuous numbers and forms (length> = 2 ). For example, N = 15 can be 1 + 2 + 3 + 4 + 5, 4 + 5 + 6, or 7 + 8. If it cannot be written as the sum of several consecutive integers, No Solution is output.

Input
Enter 1 number N (3 <=n <= 10 ^ 9 ).
OutPut
The number of 1st consecutive integers is output. If multiple integers are arranged in ascending order and cannot be decomposed into the sum of several consecutive integers, No Solution is output.
Input example
15
Output example
147 question analysis: This question has many mathematical skills. I learned the solution in the beauty of short codes. Please read the beauty of short codes. AC code:
/**  *@xiaoran  */#include<iostream>#include<cstdio>#include<map>#include<cstring>#include<string>#include<algorithm>#include<queue>#include<vector>#include<stack>#include<cstdlib>#include<cctype>#include<cmath>#define LL long longusing namespace std;int main(){int n;while(cin>>n){        stack<int> st;        int i=1;        while(n-i>1){            n-=i++;            if(n%i==0){                //cout<<n/i<<endl;                st.push(n/i);            }        }        if(st.empty()){            cout<<"No Solution"<<endl;            continue;        }        while(!st.empty()){            cout<<st.top()<<endl;            st.pop();        }}return 0;}

Appendix: calculates the number of consecutive sequences that comprise n and the composition of these sequences.

/*** The sum of consecutive numbers, the beauty of short codes * and find these combinations */# include <iostream> using namespace std; void print (int n) {// print these sequence combinations int I = 0, s = n; while (n-I> 0) {n-= I ++; if (n % I = 0) {cout <s <"="; int k, k1; for (k = 0, k1 = n/I; k <I-1; k ++, k1 ++) {cout <k1 <"+";} cout <k1 <endl ;}} int main () {int n; while (cin> n) {int sum = 0, I = 0; print (n); while (n> 0) {n-= ++ I; n % I | sum ++;} cout <sum <endl;} return 0 ;}


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.