zoukankan      html  css  js  c++  java
  • 连续自然数和

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


  • 相关阅读:
    Valid Parentheses
    3Sum
    泛型(一)
    Longest Common Prefix
    Roman to Integer
    Integer to Roman
    Container With Most Water
    知道创宇研发技能表v2.2
    anti-dns pinning 攻击
    dominator
  • 原文地址:https://www.cnblogs.com/pangblog/p/3249283.html
Copyright © 2011-2022 走看看