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动态代 ...
随机推荐
- 空值和null区别
空值代表杯子是真空的,NULL代表杯子中装满了空气
- 在Linux下配置.net网站
一.Linux安装 1.1 Linux环境 本篇文章选择VMWare虚拟机安装Linux,使用的Linux是CentOS-7.可以在百度上自行下载一个VMWare和CentOS-7镜像,建议使用最新版 ...
- [GO]并的爬取捧腹的段子
package main import ( "fmt" "strconv" "net/http" "regexp" &q ...
- logcat命令详解【一】
Android日志系统提供了记录和查看系统调试信息的功能.日志都是从各种软件和一些系统的缓冲区中记录下来的,缓冲区可以通过logcat命令来查看和使用. 在使用logcat之前,请确保手机的USB调试 ...
- ceph中pool的管理
1.创建pool 创建ceph pool的命令如下,它的参数包括pool名字.PG和PGP的数量. 若少于5个OSD, 设置pg_num为128. 5~10个OSD,设置pg_num为512. 10~ ...
- $.each()与$(selector).each()区别
jQuery.each( collection, callback(indexInArray, valueOfElement) )可用于迭代任何集合,无论是“名/值”对象(JavaScript对象)或 ...
- 关于解决百度sitemap1.0一直提示校验中问题
实际原因是php设置问题,各个版本对应的设置有些不一样. php版本改成就好了. 修复方法:在插件里找到插件:\baidusubmit\inc.找到sitemap.php,查找curl_setopt( ...
- 保证Service不被Kill的解决方案
1.Service设置成START_STICKY(onStartCommand方法中),kill 后会被重启(等待5秒左右),重传Intent,保持与重启前一样 2.通过 startForegroun ...
- 设计模式19:Chain Of Responsibility 职责链模式(行为型模式)
Chain Of Responsibility 职责链模式(行为型模式) 请求的发送者与接受者 某些对象请求的接受者可能有多种多样,变化无常…… 动机(Motivation) 在软件构建过程中,一个请 ...
- scalaWindows和Linux搭建
Windows搭建 https://www.cnblogs.com/freeweb/p/5623372.html Linux搭建 https://www.cnblogs.com/freeweb/p/5 ...