poj 1844 Sum

#include<iostream>
using namespace std;
int main()
{
int s,sum=0;
cin
>>s;
for(int i=1;;++i)
{
sum
+=i;
if(sum>=s&&(sum-s)%2==0)
{
cout
<<i<<endl;
break;
}
}
return 0;
}

  

bfs tle
#include <iostream>            //bfs  tle
#include <deque>
using namespace std;
int s;
struct node
{
int sum,t;
};
int main()
{
deque
<node> col;
node ele;
ele.sum
=1;ele.t=1;
col.push_back(ele);
ele.sum
=-1;
col.push_back(ele);
cin
>>s;
while(col.front().sum!=s)
{
node temp
=col.front();
col.pop_front();
ele.t
=temp.t+1;
ele.sum
=temp.sum+ele.t;
col.push_back(ele);
ele.sum
=temp.sum-ele.t;
col.push_back(ele);
}
cout
<<col.front().t<<endl;
return 0;
}

  

posted on 2011-07-22 20:08  sysu_mjc  阅读(97)  评论(0编辑  收藏  举报

导航