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)