It may be a number of a plurality of successive natural numbers and

Interviewer to such a problem, the input digital m, if he is and n consecutive digits, the n digital outputs, the outputs of all possible.

Example 5 = 2 + 4 + 5 = 3,15 + 6,15 = 1 + 2 + 3 + 4 + 7 + 8 = 5,15

In fact, there are three solutions to this question

The first equation solved using arithmetic sequence, i.e. the above formula, is calculated just like the line a1 is an integer, as follows:

 

#include<iostream>
using namespace std;
int main(void)
{
	int m;
	cin>>m; 
	for(int i=2;i<m/2;i++)
	{
		if((2*m-i*i+i)%(2*i)==0)
		{
			int a1=(2*m-i*i+i)/(2*i);
			if(a1>0) //有的数据会有0的情况 
			{
				for(int j=0;j<i;j++)
				{
					cout<<a1+j;
				}
				cout<<endl;
			}
		}
	} 
	return 0;
 } 

  The second way to introduce students, if your n is odd, then m% n == 0, which meet the meaning of the questions on the number n, if your n is even, then m% n == n / 2 to

#include<iostream>
using namespace std;
int main(void)
{
    int m;
    cin>>m; 
    for(int i=2;i<m/2;i++)
    {
        if(i%2!=0)
        {
            if(m%i==0)
            {
                for(int j=0;j<i;j++)
                {
                    cout<<j+m/i-(i/2);
                }    
                cout<<endl;
            }
        }
        else
        {
            if(m%i==i/2&&(m/i-(i/2)+1)>0)
            {
                for(int j=0;j<i;j++)
                {
                    cout<<j+m/i-(i/2)+1;
                }    
                cout<<endl;
            }
        }
    } 
    return 0;
 } 

The third way is to write their own violent methods of interviewing time

#include<iostream>
using namespace std;
int main(void)
{
    int m;
    cin>>m; 
    for(int i=1;i<m;i++)
    {
        int sum=0,end,j,begin;
        for(j=i;sum<m;j++)
        {
            sum+=j;
        }
        if(sum==m)
        {
            for(begin=i;begin<j;begin++)
            {
                cout<<begin;
            }
            cout<<endl;
        }
    } 
    return 0;
 } 

The above three methods themselves only after a few simple test cases, does not guarantee the right, find out what the problem may comment, I will be revised.

 

Guess you like

Origin www.cnblogs.com/caijiyang/p/12634153.html