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. Eclipse下导入外部jar包的3种方式 (zhuan)

    http://blog.csdn.net/mazhaojuan/article/details/21403717 ******************************************* ...

  2. 第二章 Python基本元素:数字、字符串和变量

    Python有哪些内置的数据类型: True False #布尔型 42 100000000 #整型 3.14159 1.0e8 #浮点型 abcdes #字符串 2.1 变量.名字和对象 pytho ...

  3. 在腾讯云上创建您的SQL Cluster(3)

    版权声明:本文由李斯达原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/250 来源:腾云阁 https://www.qclo ...

  4. python核心编程第六章练习6-8

    6-8.列表.给出一个整型值,返回代表该值得英文,比如输入89会返回“eight-nine”.附加题:能够返回符合英文语法规律的新式,比如输入89会返回“eighty-nine”.本练习中的值假定在0 ...

  5. 设置segue跳转页面

    第二种是利用ViewController与ViewController之间,拖拽添加segue 方法中提到的设置segue的identifier界面 在.h文件中声明 - (IBAction)goto ...

  6. 制作Aspose CHM文档的过程记录

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  7. openmpi出现Segmentation Fault而终止运算

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

  8. java复用和传参

    java复用和传参的三种方法总结: (1) 直接在一个类中调用函数 : 1 package test1; 2 3 public class a 4 { 5 public static void get ...

  9. 背景大图隔几秒切换(非轮播,淡入淡出)--变形金刚joy007 项目总结

    工作日想了好久,周日回家才想出来的... 图片切换(非轮播,淡入淡出) 1.切换2.停止 <html> <head> <meta content="text/h ...

  10. 关于java中JButton的样式设置(的一些我们应该知道的函数)(转)

    1. 对JButton大小的设置 ——因为JButen是属于小器件类型的,所以一般的setSize不能对其惊醒大小的设置,所以一般我们用 button.setPreferredSize(new Dim ...