简单题练习——和为S的连续正数序列

题目描述:

小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck!

输入:

输入有多组数据。

每组数据仅包括1个整数S(S<=1,000,000)。如果S为负数时,则结束输入。

输出:

对应每组数据,若不存在和为S的连续正数序列,则输出“Pity!”;否则,按照开始数字从小到大的顺序,输出所有和为S的连续正数序列。每组数据末尾以“#”号结束。

样例输入:
4
5
100
-1
样例输出:
Pity!
#
2 3
#
9 10 11 12 13 14 15 16
18 19 20 21 22
#
 
   
首位指向探查法,两个下标变化复杂度是O(n*n),移动速度有些缓慢(low和high考虑了太多情况,其实对于固定长度每个high-low只有一个解)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <assert.h>
#include <vector>
using namespace std;
int num[100000];
 
void Compute(int val)
{
    int low,high;
    low =high=1;
    int sum=1;
    bool hasAns = false;
    while (low<=val/2)
    {
        if(sum<val)
        {
            high++;
            sum+=high;
        }
        else if(sum>val)
        {
            sum-=low;
            low++;
        }
        else
        {
            hasAns =true;
            for(int i=low; i<high; i++)
                printf("%d ",i);    
            printf("%d\n",high);
            sum-=low;
            low++;
        }
    }
    if(hasAns)
        printf("#\n");
    else
        printf("Pity!\n#\n");
}
 
int main()
{
    //freopen("in.txt","r",stdin);
    int S;
    while (cin>>S && S>=0)
    {
        Compute(S); 
    }
    return 0;
}
/**************************************************************
    Problem: 1354
    User: xjbscut
    Language: C++
    Result: Accepted
    Time:430 ms
    Memory:1900 kb
****************************************************************/

如前面提到的,对于固定长度每个high-low只有一个解,线性轮训元素数目k,求出相应a0(起始元素表达式),为整数就输出,算法更高效
     
     
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <assert.h>
#include <vector>
using namespace std;
int num[100000];
 
void Compute( int val)
{
     bool hasAns = false ;
     int maxLen = ( sqrt (8.0*val+1)-1)/2;
     for ( int k=maxLen; k>=2; k--)
     {
         if ((2*val-k*k+k)%(2*k)==0)
         {
             int a0 = (2*val-k*k+k)/(2*k);
             hasAns = true ;
             for ( int i=0; i<k-1; i++)
                 printf ( "%d " ,a0+i);
             printf ( "%d\n" ,a0+k-1);
 
         }
     }
     if (hasAns)
         printf ( "#\n" );
     else
         printf ( "Pity!\n#\n" );  
}
 
int main()
{
     //freopen("in.txt","r",stdin);
     int S;
     while (cin>>S && S>=0)
     {
         Compute(S);
     }
     return 0;
}
/**************************************************************
     Problem: 1354
     User: xjbscut
     Language: C++
     Result: Accepted
     Time:140 ms
     Memory:1912 kb
****************************************************************/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值