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;

}

Problem C 链表的更多相关文章

  1. josephus Problem 中级(使用数组模拟链表,提升效率)

    问题描写叙述: 在<josephus Problem 0基础(使用数组)>中.我们提出了一种最简单直接的解决方式. 可是,细致审视代码之后.发现此种方案的效率并不高,详细体如今.当有人出局 ...

  2. Problem UVA12657-Boxes in a Line(数组模拟双链表)

    Problem UVA12657-Boxes in a Line Accept: 725  Submit: 9255 Time Limit: 1000 mSec Problem Description ...

  3. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 828C) - 链表 - 并查集

    Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...

  4. Problem E: 用链表实现约瑟夫环

    Description 你听说过约瑟夫问题吗?问题大致如下:首先n个人围成一个圈,标记为1到n号.接着,从1号开始报数(从1开始),然后2号报数,然后3号...当有人报到到m时,这个人就要踢出比赛,然 ...

  5. Problem A: C语言习题 链表建立,插入,删除,输出

    #include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct student { l ...

  6. [LeetCode] Odd Even Linked List 奇偶链表

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  7. [LeetCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

  8. [LeetCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  9. [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

随机推荐

  1. Oracle 遇到的错误及处理整理 - 记录

    1. 启动监听提示: TNS-: TNS:permission denied TNS-: TNS:protocol adapter error TNS-: Insufficient privilege ...

  2. 转!!各种数据库的jdbc驱动下载及连接方式

    各种数据库驱动 数据库名称 下载地址 说明 Mysql http://www.mysql.com/products/connector/j/ Shipped. But need to download ...

  3. 自定义view获取宽高

    View在构造函数初始化并未布局处理,此时宽高均为0,待所有控件初始化完毕后,由上级容器对内部各控件进行布局,此时控件才会具有位置与大小属性,可以通过以下方法获取:1.在ondraw()函数中获取,2 ...

  4. ToolBar Style

    colorPrimaryDark 状态栏背景色. 在 style 的属性中设置. textColorPrimary App bar 上的标题与更多菜单中的文字颜色. 在 style 的属性中设置. A ...

  5. Unity shader(CG) 写一个 散色、折射、反射、菲涅尔、gamma、简单后期屏幕特效

    http://www.lai18.com/content/506918.html 1.自生要求是很重要的,当然不是什么强迫工作之类的,而是自己有限的能力上不断的扩展兴趣上的内容. 2.用生活的眼光去发 ...

  6. MySQL和OneSQL并行插入性能对比

    按照我的作风,没图说个啥 环境 下面是受叶金荣老师的启发把相关环境交代清楚 MySQL和OneSQL的关键参数配置如下 数据库 sync_binlog innodb_flush_log_at_trx_ ...

  7. LaTeX内容总结

    欢迎关注我的社交账号: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://gith ...

  8. 在centos6.5中安装mysql5.7

    简介 博主最近在研究mysql的读写分离和主从复制,一台master和两台slave,三台机器在同一个局域网中,首先就就要在centos6.5中安装mysql5.7.好了,废话不多说,接下来进入正题. ...

  9. Sql语句统计多表个数并求和

    ) FROM ((SELECT BaseID FROM dbo.Life_cheliang WHERE BaseCreateDate BETWEEN '2015-6-5' AND '2015-6-11 ...

  10. tar.xz如何解压:linux和windows下tar.xz解压命令介绍

    在linux下怎么解压和压缩tar.xz文件? (本文由www.169it.com搜集整理) 在linux下解压tar.xz文件步骤 1 2 # xz -d ***.tar.xz  //先解压xz # ...