士兵队列训练问题

<span style="color: rgb(34, 34, 34); font-size: 16px; text-align: center;">Time Limit:</span><span id="timeLimit" style="color: rgb(34, 34, 34); font-size: 16px; text-align: center;">1000</span><span style="color: rgb(34, 34, 34); font-size: 16px; text-align: center;">MS     </span><span style="color: rgb(34, 34, 34); font-size: 16px; text-align: center;">Memory Limit:</span><span id="memoryLimit" style="color: rgb(34, 34, 34); font-size: 16px; text-align: center;">32768</span><span style="color: rgb(34, 34, 34); font-size: 16px; text-align: center;">KB     </span><span style="color: rgb(34, 34, 34); font-size: 16px; text-align: center;">64bit IO Format:</span><span id="_64IOFormat" style="color: rgb(34, 34, 34); font-size: 16px; text-align: center;">%I64d & %I64u</span>

Description

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

Input

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

Output

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

Sample Input

     
     
2 20 40
 

Sample Output

     
     
1 7 19 1 19 37

//利用数组将要删除的人置零,然后输出非零,有大神用直接看出规律了....

#include <iostream>

using namespace std;

int main()
{int n,m,i,f,sum;

cin>>n;

while(n--)
{   int a[5000]={0};
    cin>>m;
	for(i=1;i<=m;i++)
    a[i]=i;
    sum=m;
    if(m<=3)
	{cout << "1";  
            m--;  
            for(i = 2; m; i++)  
            {  
                if(a[i])  
                {  
                    cout << " " << a[i];  
                    m--;  
                }  
            }  
            cout << endl;   continue;  
	}
    while (sum)
    {f=0;
	for (i=1;i<=m;i++)
    {if(a[i])
	f++;
     if(f==2)
        {a[i]=0;
         f=0;	
         sum--;
        }
    }
	if(sum<=3)
	break;
	f=0;
    for (i=1;i<=m;i++)
    {if(a[i])
	f++;
     if(f==3)
        {a[i]=0;
         f=0;	
         sum--;
        }
    }
	if(sum<=3)
	break;
    }
            cout << "1";  
            sum--;  
            for(i = 2; sum; i++)  
            {  
                if(a[i])  
                {  
                    cout << " " << a[i];  
                    sum--;  
                }  
            }  
            cout << endl;  

}
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值