杭电1276 STL模拟

38 篇文章 0 订阅
16 篇文章 0 订阅

士兵队列训练问题

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


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

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

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

Sample Input
  
  
2 20 40
 

Sample Output
  
  
1 7 19 1 19 37


#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define lson ((root<<1)+1)
#define rson ((root<<1)+2)
#define MID ((l+r)>>1)
typedef long long ll;
typedef pair<int,int> P;
const int maxn=15000;
const int base=1000;
const int inf=999999;
int main()
{
    int n,m,i,j,k,t;
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n;
        if(n==1)
        {
            cout<<1<<endl;
            continue;
        }
        if(n==2)
        {
            cout<<1<<' '<<2<<endl;
            continue;
        }
        vector<int> v;
        vector<int>::iterator it,s,e;
        for(i=0; i<=n; i++)
            v.push_back(i);

        int p=-1;
        while(v.size()>4)
        {
            if(p==-1)
            {
                p*=-1;
                it=v.begin()+2;
                while(it<v.end())
                {
                    v.erase(it);
                    it++;
                }
            }
            else
            {
                p*=-1;
                it=v.begin()+3;
                while(it<v.end())
                {
                    v.erase(it);
                    it+=2;
                }
            }
        }
       printf("%d",*(v.begin()+1));
       for(it=v.begin()+2;it!=v.end();it++)
        printf(" %d",*it);
       printf("\n");
    }
    return 0 ;

}


 


#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define lson ((root<<1)+1)
#define rson ((root<<1)+2)
#define MID ((l+r)>>1)
typedef long long ll;
typedef pair<int,int> P;
const int maxn=15000;
const int base=1000;
const int inf=999999;
int main()
{
    int n,m,i,j,k,t;
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n;
        if(n==1)
        {
            cout<<1<<endl;
            continue;
        }
        if(n==2)
        {
            cout<<1<<' '<<2<<endl;
            continue;
        }
        vector<int> v;
        vector<int>::iterator it,s,e;
        for(i=0; i<=n; i++)
            v.push_back(i);

        int p=-1;
        while(v.size()>4)
        {
            if(p==-1)
            {
                p*=-1;
                it=v.begin()+2;
                while(it<v.end())
                {
                    v.erase(it);
                    it++;
                }
            }
            else
            {
                p*=-1;
                it=v.begin()+3;
                while(it<v.end())
                {
                    v.erase(it);
                    it+=2;
                }
            }
        }
       printf("%d",*(v.begin()+1));
       for(it=v.begin()+2;it!=v.end();it++)
        printf(" %d",*it);
       printf("\n");
    }
    return 0 ;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值