连续自然数和

2014-11-23 20:00:39 · 作者: · 浏览: 14

题目看起来比较水,但是,数学的思维还是很重要的

直接枚举TLE;

因此,我们可以枚举长度;

A、当长度为奇数的时候,中间数必定为整数

B、当长度为偶数的时候,中间数为整数 / 2(以数学角度就是存在两个中间数)


 
#include   
#include   
#include   
#include   
#include   
#include   
#include   
  
using namespace std;  
  
int main()  
{  
    int n ;  
    scanf( "%d" , &n ) ;   
    for( int i = sqrt( 2 * n ) ; i >= 2 ; i -- )  
    {  
        if( n % i == 0 && i % 2 == 1 )  
        {  
            int temp = n / i ;  
            cout << temp - i / 2  << " " << temp + i / 2 << endl ;   
        }  
        else if( 2 * n % i == 0 && ( 2 * n / i % 2 == 1 ) )  
        {  
            int temp = n / i ;  
            cout << temp - i / 2 + 1  << " " << temp + i / 2 << endl ;   
        }  
    }   
    return 0 ;  
}  

#include
#include
#include
#include
#include
#include
#include

using namespace std;

int main()
{
	int n ;
	scanf( "%d" , &n ) ; 
	for( int i = sqrt( 2 * n ) ; i >= 2 ; i -- )
	{
		if( n % i == 0 && i % 2 == 1 )
		{
			int temp = n / i ;
			cout << temp - i / 2  << " " << temp + i / 2 << endl ; 
		}
		else if( 2 * n % i == 0 && ( 2 * n / i % 2 == 1 ) )
		{
			int temp = n / i ;
			cout << temp - i / 2 + 1  << " " << temp + i / 2 << endl ; 
		}
	} 
	return 0 ;
}