这水题,真的坑

测试数据最后有空行,如果用sc.hasNextLine()判断,会RE

要改为sc.hasNext()

搞了我一上午,烦死

import java.util.*;
public class POJ1591 {
static Scanner sc = new Scanner(System.in);
static int N=20;
static class Item{
int name;
Item next;
Item pre;
}
static Item first;
static Item last;
static int[] cards;
static void count(int total,int left){
if(total<=left){
Item item = first.next;
while (item!=null){
if(item.next!=null)
System.out.print(item.name+" ");
else
System.out.println(item.name);
item=item.next;
}
return;
} int icard=0,card=0;
// 循环直到剩下left个人
while (total>left){
Item item=first.next;
card=cards[icard++]; while (item!=null){
//每次向前走cards[icard]步
int i;
for(i=1;i<card;i++){
if(item!=null)
item=item.next;
else
break;
}
if(i==card && item!=null) {
total--;
//删除第cards[cardi]个人
if (item.pre != null) {
item.pre.next = item.next;
}
if (item.next != null) {
item.next.pre = item.pre;
}
if (item == last)
last = item.pre;
item = item.next;
}
// 只剩left个人,输出结果
if(total==left){
item = first.next;
while (item!=null){
if(item.next!=null)
System.out.print(item.name+" ");
else
System.out.println(item.name);
item=item.next;
}
return;
} } }
} static void run(){
// 构建链表
first = new Item();
first.next=first.pre=null;
first.name=Integer.MIN_VALUE;
last=first;
String[] s = sc.nextLine().split(" ");
// 总人数
int n=Integer.parseInt(s[0]);
// 可以回家的人数
int left = Integer.parseInt(s[1]);
cards = new int[N];
for(int i=1;i<=n;i++){
Item item = new Item();
item.name = i;
item.next = null;
item.pre = last;
last.next = item;
last=item;
}
// 初始化卡片数组
for(int i=2;i<s.length;i++){
cards[i-2]=Integer.parseInt(s[i]);
}
// 进入计算
count(n,left);
} public static void main(String[] args) {
int so=1;
while (sc.hasNext()){
System.out.println("Selection #"+so);
run();
System.out.println();
so++;
}
}
}

  

POJ1591 M*A*S*H (JAVA)的更多相关文章

  1. [翻译]Java日志终极指南

    本文由 ImportNew - Wing 翻译自 loggly.欢迎加入翻译小组.转载请见文末要求. Java日志基础 Java使用了一种自定义的.可扩展的方法来输出日志.虽然Java通过java.u ...

  2. Ubuntu Install Java

    http://linuxpilot.com/ubuntu-java class HelloWorld{public static void main(String[]arg){System.out.p ...

  3. Java Se: Logging 框架说明

    Java Logging 用惯了log4j等日志工具,竟然不知Java还自带了个log工具.今天有空了就来了解一下. 先来看一个简单的例子: public class SystemTest { pri ...

  4. Java 集合系列11之 Hashtable详细介绍(源码解析)和使用示例

    概要 前一章,我们学习了HashMap.这一章,我们对Hashtable进行学习.我们先对Hashtable有个整体认识,然后再学习它的源码,最后再通过实例来学会使用Hashtable.第1部分 Ha ...

  5. java Channel filp compact

    import java.nio.ByteBuffer; //Listing 7-1. Copying Bytes from an Input Channel to an Output Channel ...

  6. [zz]Java中的instanceof关键字

    1.What is the 'instanceof' operator used for? stackoverflow的一个回答:http://stackoverflow.com/questions/ ...

  7. Java 动态代理机制分析及扩展

    Java 动态代理机制分析及扩展,第 1 部分 王 忠平, 软件工程师, IBM 何 平, 软件工程师, IBM 简介: 本文通过分析 Java 动态代理的机制和特点,解读动态代理类的源代码,并且模拟 ...

  8. AndroidJNI 调用JAVA(转)

    转自:http://www.cnblogs.com/likwo/archive/2012/05/21/2512400.html   1. JNIEnv对象    对于本地函数    JNIEXPORT ...

  9. JAVA中的代理技术(静态代理和动态代理)

    最近看书,有两个地方提到了动态代理,一是在Head First中的代理模式,二是Spring AOP中的AOP.所以有必要补充一下动态代理的相关知识. Spring采用JDK动态代理和CGLib动态代 ...

随机推荐

  1. qt的exe文件查找依赖的dll

    用qtcreater编译完工程生成的exe文件往往会依赖dll文件.如何一次定位exe文件所以依赖的所有dll文件呢,今天发现了软件叫hap-depends. 截图如下: 用这个软件打开exe文件就会 ...

  2. c# ftp 判断目录是否存在和创建文件夹

    工作中项目一直使用的ftp上传日志文件出现了问题,新的服务器搭建好后,日志无法上传.正好来学习一下ftp. 程序中的流程是,一个计时器,每分钟检测配置文件中本地日志文件路径下有没有日志文件,如果有就上 ...

  3. C# 实现脚本辅助功能

    http://blog.csdn.net/w86440044/article/details/42493683 http://blog.csdn.net/wujizhishang/article/de ...

  4. 商业级别Fortify白盒神器介绍与使用分析

    转自:http://www.freebuf.com/sectool/95683.html 什么是fortify它又能干些什么? 答:fottify全名叫:Fortify SCA ,是HP的产品 ,是一 ...

  5. Python守护进程(多线程开发)-乾颐堂

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  6. System.Reflection.Emit摘记

    动态类型在.net中都是用什么类型来表示的.程序集:System.Reflection.Emit.AssemblyBuilder(定义并表示动态程序集)构造函数:System.Reflection.E ...

  7. j中的substr(start,length)和substring(start,stop)

    j中的substr(start,length)和substring(start,end) substring 1 substring 方法用于提取字符串中介于两个指定下标之间的字符(包头不包尾) 2 ...

  8. twitter集成第三方登录是窗口一直出现闪退的解决方法

    需要创建自己的token,如下图

  9. 【转】C语言中DEFINE简介及多行宏定义

    要写好C语言,漂亮的宏定义是非常重要的.宏定义可以帮助我们防止出错,提高代码的可移植性和可读性等. 在软件开发过程中,经常有一些常用或者通用的功能或者代码段,这些功能既可以写成函数,也可以封装成为宏定 ...

  10. mysql 查询条件

    1简单查询   select* from 表名 select name as“姓名”fromstu     (把name改为名字) 2条件查询 where 后面跟条件  条件要写清楚 3模糊查询  l ...