zoukankan      html  css  js  c++  java
  • 51nod 1138 连续整数的和(等差数列)

    #include <iostream>
    #include <cmath>
    using namespace std;
    int n;
    int main()
    {
        ios::sync_with_stdio(false);
        while(cin>>n)
        {
            int m=sqrt(n*2)+1;
            int flag=0;
            for(int i=m;i>=2;i--)
            {
                int a=2*n-i*i+i;
                if(a%(2*i)==0&&a>0)
                {
                    flag=1;
                    cout<<a/(2*i)<<endl;
                }
            }
    
            if(!flag)
                cout<<"No Solution"<<endl;
        }
        return 0;
    }
    1138 连续整数的和
    基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
    收藏
    关注
    取消关注
    给出一个正整数N,将N写为若干个连续数字和的形式(长度 >= 2)。例如N = 15,可以写为1 + 2 + 3 + 4 + 5,也可以写为4 + 5 + 6,或7 + 8。如果不能写为若干个连续整数的和,则输出No Solution。
     
    Input
    输入1个数N(3 <= N <= 10^9)。
    Output
    输出连续整数中的第1个数,如果有多个按照递增序排列,如果不能分解为若干个连续整数的和,则输出No Solution。
    Input示例
    15
    Output示例
    1
    4
    7

    连续k个整数之和等于n,即以a为首项、公差为1的等差数列的前k项和等于n。
    所以枚举k找出首项(大于0且为整数)即可。
  • 相关阅读:
    leetcode 443: String Compression,357: Count Numbers with Unique Digits
    C++ 中 freopen()函数的用法
    filter
    map
    os.listdir
    os.path.join
    assert
    numpy中的axis和Pytorch中的dim参数
    mac中qq接收视频存放的位置
    requests
  • 原文地址:https://www.cnblogs.com/onlyli/p/7295025.html
Copyright © 2011-2022 走看看