hdu 2058 The sum problem (数学问题)

题目意思:

http://acm.hdu.edu.cn/showproblem.php?pid=2058

给出n和m,在小于n的数中,找出连续的序列,使其和为m,并从小到大输出。


题目分析:

第一次见这种题,就模拟,很显然超时了,后来在《短码之美》上看到了好的解决方法,和详细讲解,现在我只会用,不知道怎么说明白了,我也是醉了。。。


AC代码:

/**
  *@xiaoran
  */
#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<cstdlib>
#include<cctype>
#include<cmath>
#define LL long long
using namespace std;
struct node{
    int a,b;
};
int main()
{
    int n,m,a,b;
    while(cin>>n>>m){
        if((n+m)==0) break;
        stack<node> st;
        if(n>m) n=m;
        int i=0;
        while(m-i>0){
            node nd;
            m-=++i;
            if(m%i==0){
                a=m/i+1;
                b=a+i-1;
                nd.a=a; nd.b=b;
                //cout<<"["<<a<<","<<b<<"]"<<endl;
                if(b<=n) st.push(nd);
            }
            if(b>n) break;
        }
        while(!st.empty()){
            cout<<"["<<st.top().a<<","<<st.top().b<<"]"<<endl;
            st.pop();
        }
        cout<<endl;
    }
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值