POJ1591 M*A*S*H (JAVA)
这水题,真的坑
测试数据最后有空行,如果用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)的更多相关文章
- [翻译]Java日志终极指南
本文由 ImportNew - Wing 翻译自 loggly.欢迎加入翻译小组.转载请见文末要求. Java日志基础 Java使用了一种自定义的.可扩展的方法来输出日志.虽然Java通过java.u ...
- Ubuntu Install Java
http://linuxpilot.com/ubuntu-java class HelloWorld{public static void main(String[]arg){System.out.p ...
- Java Se: Logging 框架说明
Java Logging 用惯了log4j等日志工具,竟然不知Java还自带了个log工具.今天有空了就来了解一下. 先来看一个简单的例子: public class SystemTest { pri ...
- Java 集合系列11之 Hashtable详细介绍(源码解析)和使用示例
概要 前一章,我们学习了HashMap.这一章,我们对Hashtable进行学习.我们先对Hashtable有个整体认识,然后再学习它的源码,最后再通过实例来学会使用Hashtable.第1部分 Ha ...
- java Channel filp compact
import java.nio.ByteBuffer; //Listing 7-1. Copying Bytes from an Input Channel to an Output Channel ...
- [zz]Java中的instanceof关键字
1.What is the 'instanceof' operator used for? stackoverflow的一个回答:http://stackoverflow.com/questions/ ...
- Java 动态代理机制分析及扩展
Java 动态代理机制分析及扩展,第 1 部分 王 忠平, 软件工程师, IBM 何 平, 软件工程师, IBM 简介: 本文通过分析 Java 动态代理的机制和特点,解读动态代理类的源代码,并且模拟 ...
- AndroidJNI 调用JAVA(转)
转自:http://www.cnblogs.com/likwo/archive/2012/05/21/2512400.html 1. JNIEnv对象 对于本地函数 JNIEXPORT ...
- JAVA中的代理技术(静态代理和动态代理)
最近看书,有两个地方提到了动态代理,一是在Head First中的代理模式,二是Spring AOP中的AOP.所以有必要补充一下动态代理的相关知识. Spring采用JDK动态代理和CGLib动态代 ...
随机推荐
- Item2的使用
网址:http://wulfric.me/2015/08/iterm2/ 巧用 Command 键 按住⌘键: 可以拖拽选中的字符串: 点击 url:调用默认浏览器访问该网址: 点击文件:调用默认程序 ...
- Opencv Laplace算子
//通过拉普拉斯-锐化边缘 kernel = (Mat_<float>(3,3)<<1,1,1,1,-8,1,1,1,1);//Laplace算子 filter2D(img2, ...
- 不同Hadoop模式下,Hive元数据文件存储位置
假如在hive的配置文件hive-site.xml中,属性hive.metastore.warehouse.dir被设置为/root/hive/warehouse. 如果Hadoop是本地模式,则仓库 ...
- centos6.5 svn服务端搭建
一.前言 Subversion是一个免费的开源的版本管理系统,它是作为CVS(Concurrent Versions System)的取代品出现的.本文简单介绍了Subversion在centos上的 ...
- ORACLE 异机恢复
有时候需要将大的数据库发布到客户现场或转移机器时,不得不考虑在异机上恢复已经调整.测试好的库. dumpdp 全备的方法虽然易用,但在处理对象.索引.空间的时候异常的出错,比如:见有些公司,建表.索引 ...
- Word2013文章如何直接发布到CSDN博客
目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...
- lspci通过系统总线查看硬件设备信息
lspci - 列出所有PCI设备 PCI 的科普: PCI(Peripheral Component Interconnect),是一种连接电子计算机主板和外部设备的总线标准. 常见的PCI卡包括网 ...
- java程序员修炼之前笔记(前半部分)
第一部分 用java7做开发 第一章 初始java7 java7中的新特性 switch支持String 支持100_000_000数值表示法 新的异常处理 | 连接多个异常 final Except ...
- 编写高质量代码改善C#程序的157个建议——建议74:警惕线程的IsBackground
建议74:警惕线程的IsBackground 在CLR中,线程分为前台线程和后台线程,即每个线程都有一个IsBackground属性.两者在表现形式上的唯一区别是:如果前台线程不退出,应用程序的进程就 ...
- Android Paint以及ColorFilter等
我们可以通过Paint中大量的setter方法来为画笔设置属性: 这些属性大多我们都可以见名知意,很好理解,即便如此,哥还是带大家过一遍逐个剖析其用法,其中会不定穿插各种绘图类比如Canvas.Xfe ...