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. Android开发面试经——5.常见面试官提问Android题①

    版权声明:本文为寻梦-finddreams原创文章,请关注:http://blog.csdn.net/finddreams 关注finddreams博客: http://blog.csdn.net/f ...

  2. Linux配置全局环境变量的方法

    总结3种方法: 1.修改/etc/profile    1.1 :首先在此文件中设置环境变量;    1.2:export  设置好的环境变量.  #view /etc/profile export ...

  3. 青云的机房组网方案(简单+普通+困难)(虚树+树形DP+容斥)

    题目链接 1.对于简单的版本n<=500, ai<=50 直接暴力枚举两个点x,y,dfs求x与y的距离. 2.对于普通难度n<=10000,ai<=500 普通难度解法挺多 ...

  4. intel simd 资料

    http://www.cnblogs.com/zyl910/archive/2012/04/26/md00.html https://software.intel.com/sites/landingp ...

  5. 销售 >> 当今社会生产力最大的源泉为 >>自助服务 与推销员随之消失

    销售可能是变化最大的企业职能.比如,经济学家喜欢提出陷阱问题“什么概念或者想法成为当今社会生产力最大的源泉?” 大多数人回答计算机,  正确的回答是自助服务与推销员随之消失               ...

  6. testng标签运行顺序

    testng的annotations运行顺序为: @BeforeSuite @BeforeTest @BeforeClass @BeforeMethod @AfterMethod @AfterClas ...

  7. 用NAN简化Google V8 JS引擎的扩展

    通过C++扩展Google V8 JS引擎的文章很多,Google V8 JS带的例子也容易明白.但是大部分文章都是Hello World型的,真正使用时发现处处是坑.扩展V8最经典的例子就是node ...

  8. 5.6 TestNg的使用

          Java语言编写的WebDriver测试程序通常使用单元测试框架运行.TestNG单元测试框架比JUnit单元测试框架更强大,它提供了更多的扩展功能.目前很大一部分自动化测试工程师已经开始 ...

  9. hdu 2473 Junk-Mail Filter (并查集之点的删除)

    Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  10. Reverse Linked List II [LeetCode]

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...