• C语言编程练习48:士兵队列训练问题


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

    Input本题有多个测试数据组,第一行为组数N,接着为N行新兵人数,新兵人数不超过5000。
    Output共有N行,分别对应输入的新兵人数,每行输出剩下的新兵最初的编号,编号之间有一个空格。
    Sample Input
    2
    20
    40
    Sample Output
    1 7 19
    1 19 37

    链表的操作
    #include <iostream>
    #include <list>
    using namespace std;
    
    int main()
    {
        int t,n;
        cin >> t;
        while(t--)
        {
            cin>>n;
            int k;
            k=2;
            list<int >blist;
            list<int>::iterator it;
            for(int i=1;i<=n;i++)
            {
                blist.push_back(i);
            }
            while(blist.size()>3)
            {
                int num;
                num = 1;
                for(it = blist.begin();it !=blist.end();)
                {
                    if(num++%k==0)
                    {
                        it=blist.erase(it);
                    }
                    else
                    {
                        it++;
                    }
                }
                if(k==2)
                {
                    k=3;
                    continue;
                }
                else
                {
                    k=2;
                    continue;
                }
            }
                    
            for(it=blist.begin();it!=blist.end();it++)
            {
                if(it!=blist.begin())
                {
                    cout<<" ";
                }
                cout<<*it;
            }
            cout << endl;
        }
        return 0;
    }
    

     不用链表库,自己写结构体

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <queue>
    #include <stack>
    #include <algorithm>
    #include <cmath>
    #include <list>
    #include <cstdlib>
    #include <cstring>
    using namespace std;
    typedef struct _node{
        int value;
        struct _node *next;
    } Node;
    typedef  struct  _list{
        Node* head;
    }List;
    void addl(List *pList,int number){
        Node *p=(Node*)malloc(sizeof(Node));
        p->value=number;
        p->next=NULL;
        Node *last=pList->head;
        if(last){
            while(last->next){
                last=last->next;
            }
            last->next=p;
        }else{
            pList->head=p;
        }
    }
    int list_size(List *pList){
        int _size;
        _size=0;
        Node *p;
        for(p=pList->head;p;p=p->next){
            _size++;
        }
        return _size;
    }
    void print(List *pList){
        Node *p;
        for(p=pList->head;p;p=p->next){
            if(p!=pList->head){
                printf(" ");
            }
            printf("%d",p->value);
        }
        printf("
    ");
    }
    int main(){
        int t,n;
        cin>>t;
        while(t--){
            cin>>n;
            int k;
            k=2;
            List blist;
            blist.head=NULL;
            for(int i=1;i<=n;i++){
                addl(&blist,i);
            }
            while(list_size(&blist)>3){
                int num;
                num=1;
                Node *q;
                q=NULL;
                for(Node *p=blist.head;p;q=p,p=p->next){
                    if(num++%k==0){
                        if(q){
                            q->next=p->next;
                        }else{
                            blist.head=p->next;
                        }
                        free(p);
                    }
                }
                k==2 ? k=3:k=2;
            }
            print(&blist);
        }
        return 0;
    }
    

     为什么和别人的代码一样?别问!问就是灰灰考研学的!

  • 相关阅读:
    table标签去除默认边框
    自定义矢量图
    ClickJacking(点击劫持)
    css 字体不撑开默认块级元素问题
    meate 标签使用介绍
    intellij IDEA15 设置背景颜色
    JS 浮点数运算丢失精度解决方案
    IDEA 滚动条跳动问题
    JS
    异步变同步
  • 原文地址:https://www.cnblogs.com/FantasticDoubleFish/p/14381883.html
Copyright © 2020-2023  润新知