码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2058 The sum problem (数学问题)

时间:2015-01-13 19:57:28      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:hdu   数学   

题目意思:

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;
}


hdu 2058 The sum problem (数学问题)

标签:hdu   数学   

原文地址:http://blog.csdn.net/fool_ran/article/details/42679663

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!