熟用经典函数可让问题得到简化。。特别是正式需要用得时候能省下很多事情。
比如下面的例题:在执行栈和队列的操作时,,我们需要通过不同的方式,
好比链表重新建立往往浪费了时间——无效。。。
#include
#include
#include
#include
#include
using namespace std;
queue q;
int main()
{
int n,t;
scanf( "%d" , &n );
for( int i = 0 ; i < n ; i++ )
q.push(i+1);
while( !q.empty())
{
t=q.front();
q.pop();
q.push(q.front());
q.pop();
}
printf("%d",t);
puts("");
//system("pause");
return 0;
}
本以为这样可以解决杀人环的问题,但是将中间改成如下:
for( int i = 0 ; i < n ; i++ )
q.push(i+1);
while( !q.empty())
{
printf( "%d" , (q.front())'
q.pop();
q.push(q.front());
q.pop();
}
样例输入:3
样例输出:1 3 2
样例输入:7
样例输出:1 3 5 7 4 2 6
阅读(117) | 评论(0) | 转发(0) |