杭电Problem 1276 士兵队列训练问题

士兵队列训练问题

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


Problem Description
某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报到三的出列,剩下的向小序号方向靠拢,继续从头开始进行一至二报数。。。,以后从头开始轮流进行一至二报数、一至三报数直到剩下的人数不超过三人为止。
 

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

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

Sample Input
2 20 40
 

Sample Output
1 7 19 1 19 37
 
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF = 999999;
int sq[5005];
bool cmp(int x, int y)
{
	return x < y;
}
int main()
{
	int Case, n;
	while(scanf("%d", &Case) != EOF)
	{
		while (Case--)
		{
			scanf("%d", &n);
			if(n <= 3)
        	{
            	printf("1");
            	for(int i = 2; i <= n; i++)
            		printf(" %d", i);
            	printf("
");  
				continue;
			}
			for (int i = 0; i <= n; i++) {
				sq[i] = i;
			}
			for (int i = 1; sq[4] != INF; i++) {
				if (i % 2 == 1) {
					for (int j = 2; j < n; j+=2) {
						sq[j] = INF;
					}
				} else {
					for (int j = 3; j < n; j+=3) {
						sq[j] = INF;
					}	
				}
				sort(sq,sq + n,cmp);
			}
			for (int i = 1; sq[i] != INF; i++) {
				if (sq[i + 1] == INF) {
					printf("%d
", sq[i]);
				}
				else printf("%d ", sq[i]);
			}
		}
	}
	
	return 0;
}


原文地址:https://www.cnblogs.com/cniwoq/p/6770974.html