zoukankan      html  css  js  c++  java
  • hdu 1868 Consecutive sum

    hdu 1868 Consecutive sum

    Problem DescriptionEvery body knew that 15 = 1+2+3+4+5 = 4+5+6 = 7+8. Now give you a number N, tell me how many ways to represent N as a sum of consecutive positive integers. For example, 15 have 3 ways to be found.  InputEach line will contain an signed 32-bits integer N. Process to end of file.  OutputFor each case, output the answer in one line.  Sample Input151050  Sample Output311 注: 此题主要公式 (x+y)*(y-x+1)/2=n #include<iostream>
    #include<cmath>
    using namespace std;
    int main()
    {
     int n,num,i,j,x,y;
     while(cin>>n)
     {
      num=0;
      for(i=(int)sqrt(2*n);i>=2;i--)
      {
       if(2*n%i==0)
       {
        x=(2*n/i-i+1)/2;    
        y=(2*n/i+i-1)/2;
        if((y+x)*(y-x+1)==2*n)
        {
         num++;
        }
       }
      }
      cout<<num<<endl;
     }
        return 0;
    }  
  • 相关阅读:
    git学习
    Command Line
    python之测试
    python之模块
    python之函数
    python之类
    python之错误和异常
    python之迭代器和生成器
    python之字典和集合
    python之列表和元组
  • 原文地址:https://www.cnblogs.com/crazyapple/p/2999509.html
Copyright © 2011-2022 走看看