设为首页 加入收藏

TOP

连续自然数和
2014-11-23 20:00:39 来源: 作者: 【 】 浏览:12
Tags:连续 自然

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

直接枚举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 ;
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇日志文件C++ 时间 文件 行数 下一篇hdu 2203 亲和串

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Python爬虫教程(从 (2025-12-26 16:49:14)
·【全269集】B站最详 (2025-12-26 16:49:11)
·Python爬虫详解:原 (2025-12-26 16:49:09)
·Spring Boot Java: (2025-12-26 16:20:19)
·Spring BootでHello (2025-12-26 16:20:15)