这水题,真的坑

测试数据最后有空行,如果用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. 用map函数来完成Python并行任务的简单示例

    众所周知,Python的并行处理能力很不理想.我认为如果不考虑线程和GIL的标准参数(它们大多是合法的),其原因不是因为技术不到位,而是我们的使用方法不恰当.大多数关于Python线程和多进程的教材虽 ...

  2. winfrom 循环播放图片

    没啥新东西了,就是遍历和匹配文件名然后获取对象,放到picturebox里面 选中listview中想要查看的图片,然后点击查看按钮,进行↓代码. if (listView1.SelectedItem ...

  3. oo原则

    基本原则: 封装变化Encapsulate what varies. 面向接口编程而非实现 Code to an interface rather than to an implementation. ...

  4. 初学者教程之命名空间,范围解析及LEDB规则

    2014年5月12日 Sebastian Raschka编写 这是一篇关于采用LEGB规则实现Python变量命名空间及范围解析的简短教程.下面章节将会提供简短的可以说明问题的示例代码块来简要阐述问题 ...

  5. 在OpenSSL中添加自定义加密算法

    一.简介 本文以添加自定义算法EVP_ssf33为例,介绍在OpenSSL中添加自定义加密算法的方法 二.步骤 1.修改crypto/object/objects.txt,注册算法OID,如下: rs ...

  6. LWIP数据包管理

  7. Qt的翻译文件QTranslator不能使用问题总结(原)

    笔者今天在自己工程中使用翻译文件,发现没有起作用,反复查找,发现是用宏定义了命名空间,生成.ts文件时,不会加上命名空间,所以生成的.qm文件在实际使用时,会无法找到对应的语句. 如果将宏定义的命名空 ...

  8. javascrip总结43:标签上自定义属性的操作

    1 获取标签属性 语法: element.getAttribute('属性名') 返回对应属性的值 ,如果没有返回null. //html <div id="box" ind ...

  9. CodeForces 474B Worms (水题,二分)

    题意:给定 n 堆数,然后有 m 个话询问,问你在哪一堆里. 析:这个题是一个二分题,但是有一个函数,可以代替写二分,lower_bound. 代码如下: #include<bits/stdc+ ...

  10. 【转载】python计算文件的行数和读取某一行内容的实现方法

    一.计算文件的行数 最简单的办法是把文件读入一个大的列表中,然后统计列表的长度.如果文件的路径是以参数的形式filepath传递的,那么只用一行代码就可以完成我们的需求了: count = len(o ...