您的位置:首页 > 其它

hdu 2058 The sum problem (数学问题)

2015-01-13 17:55 393 查看
题目意思:

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: