51nod 1138 consecutive integers of the and (mathematics)

Source: Internet
Author: User

Title Description:

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1138

A positive integer n is given, and N is written as a number of consecutive numbers and forms (length >= 2). For example, n = 15, can be written as 1 + 2 + 3 + 4 + 5, can also be written as 4 + 5 + 6, or 7 + 8. If it cannot be written as a number of consecutive integers, the output is no solution.

Input
Enter 1 number n (3 <= n <= 10^9).
OutPut
Outputs the 1th number in a continuous integer, and if there are more than one order of ascending order, the output no solution if it cannot be decomposed into a number of consecutive integers.
Input Example
15
Output Example
147 Topic Analysis: This problem has a lot of math skills, I was in the "Short Code of the United States" to learn the solution, please see the "Short Code of the United States." 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;}

Appended: The number of sequential sequences that comprise N, and the composition of these sequences.

/** * Number of consecutive numbers, short code of the beauty problem * 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;}


51nod 1138 consecutive integers of the and (mathematics)

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.