士兵队列训练问题
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2668    Accepted Submission(s): 1216


Problem Description
某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:

从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,

凡报到三的出列,剩下的向小序号方向靠拢,继续从头开始进行一至二报数。。。,以后从头开始

轮流进行一至二报数、一至三报数直到剩下的人数不超过三人为止。

 

Input
本题有多个测试数据组,第一行为组数N,接着为N行新兵人数,新兵人数不超过5000。

 

Output
共有N行,分别对应输入的新兵人数,每行输出剩下的新兵最初的编号,编号之间有一个空格。

 

Sample Input
2
20
40
 

Sample Output
1 7 19
1 19 37
 

Author
Cai Minglun
 

Source
杭电ACM集训队训练赛(VI)
 

Recommend
lcy

AC的代码:
 
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "malloc.h"
struct queue
{
 int capacity;  //容量
 int size;    //当前大小
 int *array; //存取编号的数组
 int tail; //队尾
 int end; //队头
};

int init_queue(queue *q)    //初始化
{   
    q->size=0;
    q->tail=0;
    q->end=0;
    return 1;
   }
  
int init_queue (queue* q,int capacity) //创建一个队列
{
    q->capacity=capacity;
    q->size=0;
    q->tail=0;
    q->end=0;
    q->array=(int *)malloc(sizeof(int)*capacity);
    memset(q->array,0,sizeof(q->array));
    return 1;
   
    }

int get_size(queue *q)   //当前队列大小
{
    return q->size;
}

int get_capacity(queue *q) //队列的容量
{
    return q->capacity;
}

int add(queue *q,int value) //t添加队列元素
{
    q->array[q->end]=value;
    q->end++;
    q->size++;
    return value;
    }
   
int pull(queue *q)  //c出队列
{
    int value=q->array[q->tail];
    q->tail++;
    q->size--;
    return value;
    }
   
int peek(queue *q)   
{
    return q->array[q->tail];
}

int freequeue(queue*q) //销毁队列
{
    free(q);
    return 1;
}

   
    int main()
    {
        queue *sq=NULL;  //队列1
        queue *eq=NULL;  //队列2
        int cases,i,n,j,num=2;
        scanf("%d",&cases);
        while(cases-->0)
      {
         sq=(queue *)malloc(sizeof(queue));  //给队列1分配空间
          eq=(queue *)malloc(sizeof(queue)); //给队列2分配空间
          scanf("%d",&n);             
           init_queue(sq,n);   //给队列1初始化
          init_queue(eq,n);  //给队列2初始化
       
        for(i=1;i<=n;i++)    //为队列1添加元素
        {
          add(sq,i);
        }
        num=2;
        while(1&&n>3)
        {
             i=1;
             if(num==2)
             {
                   while(sq->size>0)
                    {
                                    if(i!=num)    //不符合条件的进入队列2
                                    {
                                              add(eq,pull(sq));
                                              ++i;
                                    }
                                    else
                                    {            //符合条件的出队列
                                        pull(sq);
                                        i=1;   //注意  i值 返回成 1
                                       
                                    }
                    }
                    if(eq->size <=3)     //如果剩余 元素 满足跳出条件 就终止循环
                    {
                                break;
                    }
                    num=3;
                    init_queue(sq);
              }
              else if(num==3)
              {
             
                   while(eq->size>0)       //同上
                    {
                                    if(i!=num)
                                    {
                                              add(sq,pull(eq));
                                              ++i;
                                    }
                                    else
                                    {
                                        pull(eq);
                                        i=1;
                                       
                                    }
                    }
                    if(sq->size <=3)
                    {
                                break;
                    }
                    num=2;
                    init_queue(eq);
                    } 
              }
                    
                 if(eq->size==0)   //跳出循环后 判断要输出那个队列的元素
                 {
                               while(sq->size>1)
                               {
                                                printf("%d ",pull(sq));
                                               
                               }
                               printf("%d\n",pull(sq));
                              
                 }
                  else
                  {
                      while(eq->size>1)
                      {
                                       printf("%d ",pull(eq));
                      }
                      printf("%d\n",pull(eq));  
                    
                    
                  }
               free(sq); //使用后释放申请的空间
               free(eq);  //使用后释放申请的空间
               }
              
              return 0;
             
        }
   
   
   

posted on 2013-08-13 16:32  守护生命的绿荷  阅读(435)  评论(0编辑  收藏  举报