Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5279
  • 博文数量: 6
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 67
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-14 19:47
文章分类

全部博文(6)

文章存档

2014年(6)

我的朋友

分类: C/C++

2014-04-14 21:30:09

熟用经典函数可让问题得到简化。。特别是正式需要用得时候能省下很多事情。

比如下面的例题:在执行栈和队列的操作时,,我们需要通过不同的方式,

好比链表重新建立往往浪费了时间——无效。。。

 

#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) |
给主人留下些什么吧!~~