zoukankan      html  css  js  c++  java
  • Problem C 链表

    Description

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

    Input

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

    Output

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

    Sample Input

    2 20 40
     

    Sample Output

    1 7 19 1 19 37
     
    分析:
      用一个数组将其存下,第一次将下标为偶数的保存,下标为奇数的出列,第二次将i%3==2的数消掉,
    然后又按着第一,二次这样循环往复,当数的个数小于,等于3时,输出为被消掉的数的编号。
     
     

    #include <iostream>
    #include <cstring>
    #include <list>
    const int maxn=5000+5;
    using namespace std;
    int main()
    {
      int t;
      scanf("%d",&t);
      while (t--)
      {
        int n, a[maxn];
        memset(a, 1, sizeof(a));
        scanf("%d",&n);
        for (int i = 1; i <= n; i++)
        a[i] = i;
        int k = n;
        int loge = 2;
        while (k > 3)
        {
          int q = 0;
          if (loge == 2)
          {
            for (int i = 1; i <= n; i++)
            if (a[i] != 0 && ++q == 2)
            {
              q = 0;
              a[i] = 0;
              k--;
            }
              loge = 3;
          }
          else
          {
            for (int i = 1; i <= n; i++)
            if (a[i] != 0)
            {
              if (a[i] != 0 && ++q == 3)
              {
                q = 0;
                a[i] = 0;
                k--;
              }
            }
            loge = 2;
          }

        }
        loge = 1;
        for (int i = 1; i <= n; i++)
        if (a[i] != 0)
        if (loge)
        {
          cout << i;
          loge = 0;
        }
        else
          cout << ' ' << i;

         cout << endl;
      }

    return 0;

    }

  • 相关阅读:
    linux的软连接和硬连接
    各种Python简单功能代码
    《财报就像一本故事书》刘顺仁(二) ——财务报表
    Atitit .h5文件上传 v3
    Atitti. 语法树AST、后缀表达式、DAG、三地址代码
    Atitit.在线充值功能的设计
    Atitit。数据库 安全性 重要敏感数据加密存储解决方案
    atitit.数据验证db数据库数据验证约束
    Atitit.提升电子商务安全性 在线充值功能安全方面的设计
    Atitit.antlr实现词法分析
  • 原文地址:https://www.cnblogs.com/xl1164191281/p/4676430.html
Copyright © 2011-2022 走看看