连续自然数和

http://wikioi.com/problem/1312/

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

直接枚举TLE;

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

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

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<iomanip>

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 ;
}


原文地址:https://www.cnblogs.com/pangblog/p/3249283.html