zoukankan      html  css  js  c++  java
  • uva 133 The Dole Queue

    题意:http://www.nocow.cn/index.php/UVA_133(中文翻译)

    解题思路:双向循环链表,模拟操作即可

    解题代码:

    View Code
    // File Name: uva133.cpp
    // Author: darkdream
    // Created Time: 2013年04月27日 星期六 09时50分42秒
    
    #include<vector>
    #include<list>
    #include<map>
    #include<set>
    #include<deque>
    #include<stack>
    #include<bitset>
    #include<algorithm>
    #include<functional>
    #include<numeric>
    #include<utility>
    #include<sstream>
    #include<iostream>
    #include<iomanip>
    #include<cstdio>
    #include<cmath>
    #include<cstdlib>
    #include<cstring>
    #include<ctime>
    
    using namespace std;
    struct node
    {
        int date;
        struct node *next ,*last;
    };
    int main(){
        int n,k,m;
        while(scanf("%d %d %d",&n,&k,&m) != EOF)
        {
            //k = k%(n+1); m = m%(n+1);
            if(n == 0)
                break;
            struct node *head,*p,*q;
            struct node a[30];
            for(int i = 1; i<= n;i ++)
            {  a[i].date = i;
                a[i].next = &a[i+1];
                a[i].last = &a[i-1];
            }
            a[1].last = &a[n];
            a[n].next = &a[1];
            p = &a[1];
            q = &a[n];
            while(1)
            {   
                
                if(n == 1)
                { 
                    printf("%3d\n",p->date);
                    break;
                }
               for(int i= 1; i< (k%n==0?n:k%n); i ++)
                {
                    p = p->next;
                }
                for(int i = 1; i< (m%n==0?n:m%n) ; i++)
                {
                    q = q->last;
                }
                if(p->next == q && p->last == q && p != q)
                {
                   printf("%3d%3d\n",p->date,q->date);
                   break;
                }
                if(p == q)
                {
                   
                    printf("%3d,",p->date);
                    p->next->last = p->last;
                    p->last->next = p->next;
                    p = p->next;
                    q = q->last;
                    n--;
                   
                }
                else{
                  printf("%3d%3d,",p->date,q->date);
                  p->last->next = p->next;
                  p->next->last = p->last;
                  if(p->next == q)
                      p = q->next;
                  else
                    p= p->next;
                  q->last->next = q->next;
                  q->next->last = q->last;
                  q = q->last;
                  n = n-2;
                    
                }
                  
    
            }
    
    
        }
        return 0;
    }
    没有梦想,何谈远方
  • 相关阅读:
    万恶之源-03-基本数据类型(int, bool, str)
    万恶之源- 02 运算符和编码
    MSDN地址,记录下来,以防以后使用
    ER图与UML图
    js清空页面控件值
    JQuery一句话实现全选/反选
    jQuery判断checkbox是否选中的3种方法
    未能加载文件或程序集“”或它的某一个依赖项。系统找不到指定的文件
    关于Resharper的使用经验
    24种设计模式的体验
  • 原文地址:https://www.cnblogs.com/zyue/p/3047339.html
Copyright © 2011-2022 走看看