HDU1276

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

 

Description

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

Input

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

Output

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

Sample Input

2 20 40
 

Sample Output

1 7 19 1 19 37
 
 
题解:本来题目是用链表的方法,本人愚钝,不会用,看了别人的博客才明白,在最后展示
我采用模拟的方法,即直接按照题意写出。
         要注意最后输出的格式!
 
 
 
下面这个代码是错误的。很久才找到错误所在。只是一个小小的错误(最下面有AC的代码)
#include<iostream>
using namespace std;
int main()
{
int i,t,n,p;
int f=;
int a[];
cin>>t;
while(t--)
{
int q;//定义整型变量,n赋值给q
cin>>n;
q=n;
for(i=; i<=n; i++)
a[i]=i;
while(q>) //判断条件
{ p=;
for(i=; i<=n; i++)
{
if(a[i]==)
continue;
else
p++;
if(p==)
{
a[i]=;
f+=; //统计出列士兵个数
p=;
}
}
q=q-f;
if(q<=)break;
p=;
f=; //注意每次归0
for(i=; i<=n; i++)
{
if(a[i]==)
continue;
else
p++;
if(p==)
{
a[i]=;
f+=;
p=;
}
}
q=q-f;
}
p=;
for(int i=; i<=n; i++)
{
if(a[i]==)
continue;
else
{
p+=;
if(p==q)
cout<<a[i]<<endl;
else
cout<<a[i]<<" ";
}
}
}
return ; }

这个才是正确的,错误的地方有标记

#include<iostream>
using namespace std;
int main()
{
int i,t,n,p;
int a[];
cin>>t;
while(t--)
{
int q;//定义整型变量,n赋值给q
cin>>n;
q=n;
for(i=; i<=n; i++)
a[i]=i;
while(q>) //判断条件
{
int f=;//这里!!
p=;
for(i=; i<=n; i++)
{
if(a[i]==)
continue;
else
p++;
if(p==)
{
a[i]=;
f+=; //统计出列士兵个数
p=;
}
}
q=q-f;
if(q<=)break;
p=;
f=; //注意每次归0
for(i=; i<=n; i++)
{
if(a[i]==)
continue;
else
p++;
if(p==)
{
a[i]=;
f+=;
p=;
}
}
q=q-f;
}
p=;
for(int i=; i<=n; i++)
{
if(a[i]==)
continue;
else
{
p+=;
if(p==q)
cout<<a[i]<<endl;
else
cout<<a[i]<<" ";
}
}
}
return ; }

参考小云博客:链表使用,与模拟还是很相像的

#include"iostream"
#include"list"
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
list<int> s;
if(n==) cout<<<<endl;
else
{
for(int i=; i<=n; i++)
s.push_back(i);
list<int>::iterator it,ip;//链表遍历迭代器
int f=;
int flag=s.size();
while(flag>)
{
for(it=s.begin(); it!=s.end();)
{
if(f==)
{
f=;
ip=it;
it++;
s.erase(ip); //抹除操作
flag--;
}
else
{
f++;
it++;
}
}
f=;
if(flag<=) break;
for(it=s.begin(); it!=s.end();)
{
if(f==)
{
f=;
ip=it;
it++;
s.erase(ip);
flag--;
}
else
{
f++;
it++;
}
}
f=;
if(flag<=) break;
}
int w=;
for(it=s.begin(); it!=s.end(); it++)
{
cout<<(*it); //注意输出格式
w++;
if(w<=flag-) cout<<' ';
}
cout<<endl;
}
}
return ;
}

HDU1276(士兵队列训练模拟与链表)的更多相关文章

  1. hdu1276士兵队列训练问题[简单STL list]

    目录 题目地址 题干 代码和解释 题目地址 hdu1276 题干 代码和解释 本题使用了STL中的list,STL的list是双向链表.它的内存空间不必连续,通过指针来进行数据的访问,高效率地在任意地 ...

  2. (hdu step 8.1.6)士兵队列训练问题(数据结构,简单模拟——第一次每2个去掉1个,第二次每3个去掉1个.知道队伍中的人数&lt;=3,输出剩下的人 )

    题目: 士兵队列训练问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  3. HDU 1276 士兵队列训练问题(模拟)

    原题代号:HDU 1276 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1276 题目原题: 士兵队列训练问题 Time Limit: 2000/10 ...

  4. hdoj 1276 士兵队列训练问题【模拟】

    士兵队列训练问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  5. A - 士兵队列训练问题

    A - 士兵队列训练问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

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

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

  7. UVa 12657 Boxes in a Line(数组模拟双链表)

    题目链接 /* 问题 将一排盒子经过一系列的操作后,计算并输出奇数位置上的盒子标号之和 解题思路 由于数据范围很大,直接数组模拟会超时,所以采用数组模拟的链表,left[i]和right[i]分别表示 ...

  8. 【ACM】hdu_1276_士兵队列训练问题_201308131032

    士兵队列训练问题Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  9. HDU 1276 士兵队列训练问题(队列)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1276 题目: 士兵队列训练问题 Time Limit: 2000/1000 MS (Java/Othe ...

随机推荐

  1. [JAVA关键字] synchronized

    synchronized, Example: public synchronized void XXX() {} 参考 http://wenku.baidu.com/link?url=ecb1Zivf ...

  2. [Locked] Zigzag Iterator

    Zigzag Iterator Given two 1d vectors, implement an iterator to return their elements alternately. Fo ...

  3. Gridview实现银行选择列表

    [MainActivity.java] package com.example.activitydemo; import android.os.Bundle; import android.view. ...

  4. php 自定义求数组差集,效率比自带的array_diff函数还要快(转)

    <?phpfunction array_different($array_1, $array_2) { $array_2 = array_flip($array_2); //将数组键值调换 fo ...

  5. TCP/IP源码(59)——TCP中的三个接收队列

    http://blog.chinaunix.net/uid-23629988-id-3482647.html TCP/IP源码(59)——TCP中的三个接收队列  作者:gfree.wind@gmai ...

  6. GCOV 使用用例

      1.GCOV查看arm-linux代码覆盖率 一.           关于gcov工具 gcov伴随gcc 发布.gcc编译加入-fprofile-arcs -ftest-coverage 参数 ...

  7. qt 关于内存泄漏的检测

    Qt 关于内存泄露的检测: 收藏人:guitarhua     2012-02-10 | 阅:  转:    |   来源   |  分享               Qt 关于内存泄露的检测:工具篇 ...

  8. ubuntu12.10中没有/etc/inittab文件探究

    1. 我们首先来看一下Linux系统开机启动过程: Ubuntu是Linux系统的衍生系统,其开机启动过程与上图相差不大,但是随着系统的不断发展,终究还是有不同的地方,下面,我们来了解一下Ubuntu ...

  9. Getting Started with the NDK

    The Native Development Kit (NDK) is a set of tools that allow you to leverage C and C++ code in your ...

  10. 大牛对ACM入门菜鸟的一些话

    首先就是我为什么要写这么一篇日志.原因很简单,就是因为前几天有个想起步做ACM人很诚恳的问我该如何入门.其实就现在而言,我并不是很想和人再去讨论这样的话题,特别是当我发现我有很多的东西要学的时候,我实 ...