• 士兵队列训练问题http://acm.hdu.edu.cn/showproblem.php?pid=1276


    士兵队列训练问题

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2663    Accepted Submission(s): 1211


    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
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    #include <string.h>
    struct queue
    {
    	int capacity;
    	int size;
    	int *array;
    	int tail;
    	int end;
    };
    int add(queue *q,int value)
    {
    	q->array[q->end] = value;
    	q->end++;
    	q->size++;
    	return value;
    }
    int peek(queue *q)
    {
    	return q->array[q->tail];
    }
    int pull(queue *q)
    {
    	int value = q->array[q->tail];
    	q->array[q->tail] = 0;
    	q->tail++;
    	q->size--;
    	return value;
    }
    
    int get_size(queue *q)
    {
    	return q->size;
    }
    
    int get_capacity(queue *q)
    {
    	return q->capacity;
    }
    
    int init_queue(queue *q, int capacity)
    {
    	q->capacity = capacity;
    	q->size = 0;
    	q->tail = 0;
    	q->end = 0;
    	q->array = (int*)malloc(capacity*sizeof(int));
    	memset(q->array,0,sizeof(q->array));
    	return 1;
    }
    int init_queue(queue *q)
    {
    	q->size = 0;
    	q->tail = 0;
    	q->end = 0;
    	return 1;
    }
    int free_queue(queue *q)
    {
    	free(q);
    	return 1;
    }
    
    int main()
    {
      queue* sq = NULL;
      queue* eq = NULL;
      int cases,i,n,j,num=2;
      scanf("%d",&cases);
      while(cases-->0)
      {
    		sq = (queue*)malloc(sizeof(queue));
    		eq = (queue*)malloc(sizeof(queue));
    		scanf("%d",&n);
    		init_queue(sq,n);
    		init_queue(eq,n);
    		for(i=1; i<=n; ++i)
    		{
    			add(sq,i);
    		}
    		num = 2;
    		while(1 && n>3)
    		{
    			i=1;
    			if(num==2)
    			{
    				while(sq->size > 0)
    				{
    					if(i!=num)
    					{
    						add(eq,pull(sq));
    						++i;
    					}
    					else
    					{
                            pull(sq);
                            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
    ",pull(sq));
    		}
    		else
    		{
                while(eq->size>1)
    			{
    				printf("%d ",pull(eq));
    			}
    			printf("%d
    ",pull(eq));
    		}
    		free(sq);
    		free(eq);
    	}
      system("PAUSE");
      return 0;
    }
    
    这是用队列写的程序,我还不太会队列,所以这也不是我写的,这是学长帮我们写的,虽说听他讲了一遍,但一顿饭之后我忘得一干二净。所以懂得行家可以看一下。下面是我自己写的,不会队列的朋友们,可以看一下下面的代码:
    #include<stdio.h>
    #include<string.h>
    #define M 5000
    int main()
    {
        int a[M+10],b[M+10],c[M+10];
        int n;
        while(scanf("%d",&n)!=EOF)
        {
    		while(n--)
    		{
    			int i,j,num,count=2;
    			scanf("%d",&num);
    			for(i=1;i<=num;i++)
    				a[i]=i;
    			if(num==0)
    				printf("%d
    ",0);
    			else if(num==1)
    				printf("%d
    ",1);
    			else if(num==2)
    				printf("%d %d
    ",1,2);
    			else if(num==3)
    				printf("%d %d %d
    ",1,2,3);
    			else
    			{
    				while(1)
    				{
    					if(count==2)
    					{
    						 memset(b,0,sizeof(b));
    						 memset(c,0,sizeof(c));                    
    						 for(j=1,i=1;i<=num;i++)
    						 {
    							 if((i%2)!=0)
    							 {
    								c[j]=b[j]=a[i];
    								j++;
    							  }
    						  }
    						 num=j-1;
    						  count=3;
    						  if(j<=4)
    							  break;
    					}
    					else if(count==3)
    					{
    						 memset(a,0,sizeof(a));
    						 for(j=1,i=1;i<=num;i++)
    						 {
    							 if((i%3)!=0)
    							 {
    								c[j]=a[j]=b[i];
    								j++;
    							 }
    						 }
    						 num=j-1;
    						 count=2;
    						 if(j<=4)
    							break;                    
    					}
    				}
    				for(i=1;i<=num;i++)
    				{
    					if(i!=num)
    						printf("%d ",c[i]);
    					else
    						printf("%d
    ",c[i]);    
    				}
    			}
    		}
        }
        return 0;
    }
    


    其实和队列的思想一样,先进先出,我只不过是用了两个数组实现了这个操作,大家可以看一下。

  • 相关阅读:
    [Compose] 12. Two rules about Funtors
    [Compose] 11. Use Task for Asynchronous Actions
    [React Native] Animate Styles of a React Native View with Animated.timing
    [Compose] 10. Capture Side Effects in a Task
    [Angular Router] Lazy loading Module with Auxiliary router
    注重实效的程序员——途径篇
    UVa 674: Coin Change
    apache POI 导出excel相关方法
    Java调用R(二)_JRI
    drp用户管理完成后,asp.net与java的一个简单比较
  • 原文地址:https://www.cnblogs.com/wangyouxuan/p/3254990.html
Copyright © 2020-2023  润新知