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. 数据库函数--nvl、coalesce、decode比较

    SQL中 nvl().coalesce().decode()这三个函数nvl(bonus,0) 2个参数 if bonus is null return 0 else return bonus,ora ...

  2. WAMP 403 Forbidden禁止访问 的解决办法

    修改httpd.conf文件 # onlineoffline tag - don't remove Require local To # onlineoffline tag - don't remov ...

  3. Java GC系列(4):垃圾回收监视和分析

    本文由 ImportNew - lomoxy 翻译自 javapapers. 目录 垃圾回收介绍 垃圾回收是如何工作的? 垃圾回收的类别 垃圾回收监视和分析 在这个Java GC系列教程中,让我们学习 ...

  4. JAVA 数组作业——动手动脑以及课后实验性问题

    JAVA课后作业——动手动脑 一:阅读并运行示例PassArray.java,观察并分析程序输出的结果,小结,然后与下页幻灯片所讲的内容进行对照. 1.源代码 // PassArray.java // ...

  5. python与unicode

    Unicode是一种在计算机上使用的字符编码,是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,以满足跨语言.跨平台进行文本转换.处理的要求. Uni ...

  6. Visual Studio中的快捷键

    我们在使用Visual Studio的时候,如用一些快捷键,就能减少我们键盘和鼠标来回切换的次数,从而提高我们编码的速度,在此跟大家分享一些经常Visual Studio中用到的快捷键 自动缩进:选中 ...

  7. springMvc配置编码过滤器

    在web.xml中配置 <!-- 编码过滤器 --> <filter> <filter-name>characterEncodingFilter</filte ...

  8. 转载:Javascript作用域原理

    首先看一个例子: var name = 'laruence'; function echo() { alert(name); var name = 'eve'; alert(name); alert( ...

  9. 《Play for Java》学习笔记(四)Controller

    play的一大优势是可以将HTTP映射到JAVA API代码(Type-safe mapping from HTTP to an idiomatic Scala or Java API),完美的实现了 ...

  10. eclipse 项目修改和更新项目,回退版本,解决分支的冲突的办法

    一个关于git的图 1.我在github建立了3个分支. 2.把其中一个分支拉到本地. 项目修改提交到远程库 3.修改完代码以后commit项目,点击项目右击->team->commit ...