zoukankan      html  css  js  c++  java
  • J

    Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequences that the sum of the sub-sequence is M.

    InputInput contains multiple test cases. each case contains two integers N, M( 1 <= N, M <= 1000000000).input ends with N = M = 0. 
    OutputFor each test case, print all the possible sub-sequence that its sum is M.The format is show in the sample below.print a blank line after each test case. 
    Sample Input

    20 10
    50 30
    0 0

    Sample Output

    [1,4]
    [10,10]
    
    [4,8]
    [6,9]
    [9,11]
    [30,30]

    数学思维题目,,看了一下大佬的博文。。感觉 还复杂啊!!
    #include<iostream>
    #include<algorithm>
    #include<string> 
    #include<cstdio>
    #include<math.h>
    using namespace std;
    /*
    思路:
    区间项数为i
    起始位置 a,终点为b则b=a+i-1;
    这一段区间的和为sum=(a+b)*i/2=(a+a+i-1)*i/2=m;
    a最小为1,则(1+1+i-1)*i/2<=m即(i+1)*i<=2m , i有一定小于sqrt(2m)
    即 0<i<sqrt(2m);判断条件就是 (a+a+i-1)*i==2*m;
    */
    int main()
    {
        int n,m,a,b;
        while(cin>>n>>m)
        {
            if(n==0 && m==0)
                break;
            for(int i=sqrt(2*m);i>=1;i--)//最小为1项,就是[m,m] 
            {
                a=m/i-(i-1)/2;//等差公式 即 m=a*i+i*(i-1)/2-----m/i=a+(i-1)/2---a=m/i-(i-1)/2;
                if((a+a+i-1)*i==2*m)
                {
                    printf("[%d,%d]
    ",a,a+i-1);
                }
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    error LNK2019: 无法解析的外部符号 该符号在函数 中被引用 解决方案
    【OSG】运行OSG示例出现的奶牛不完整问题
    python 遍历文件夹
    python os操作
    python io操作
    python request 代理/超时/证书
    python tuple
    python dict
    python request post
    python request get
  • 原文地址:https://www.cnblogs.com/Accepting/p/11208440.html
Copyright © 2011-2022 走看看