题目链接

题意

两个人玩扑克,共n张牌,第一个人k1张,第二个人k2张

给定输入的牌的顺序就是出牌的顺序

每次分别比较两个人牌的第一张,牌上面数字大的赢,把这两张牌给赢的人,并且大的牌放在这个人的牌最下面,另外一张放在上面牌的上面,其他牌在放在这两张牌的上面。

求要pk多少次结束游戏,并记录赢得是哪个人

若出现死循环的情况输出 –1

 

这里可以根据栈或队列

java的程序是根据栈的,比较时候取出栈顶,加入新的两个 数的时候,要先出栈,在入栈,有点麻烦

Python程序是根据队列,在头取出进行比较,加入时候再队尾加入元素,不会出现过度的入栈和出栈的操作

Java 的有增加了队列实现

ArrayList可实现队列的功能,比较简单了

Java程序

import java.awt.List;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack; public class C546 {
static void run1(){
Scanner in =new Scanner(System.in);
PrintStream out = System.out;
int n = in.nextInt();
int Maxtime =1000;
ArrayList<Integer> l1 = new ArrayList<Integer>();
ArrayList<Integer> l2 = new ArrayList<Integer>();
int k1 = in.nextInt();
for(int i=1;i<=k1;i++) l1.add(in.nextInt());
int k2 = in.nextInt();
for(int i=1;i<=k2;i++) l2.add(in.nextInt());
int count =0;
while(l1.size()>0 && l2.size()>0 &&Maxtime>0){
count++;
Maxtime--;
int q1 = l1.get(0);
int q2 = l2.get(0);
l1.remove(0);
l2.remove(0);
if(q1> q2){
l1.add(q2);
l1.add(q1);
}else{
l2.add(q1);
l2.add(q2);
}
}
if(Maxtime==0)
out.println(-1);
else
out.println(count+" "+(l1.size()>0?1:2));
}
static void run(){
Scanner in = new Scanner(System.in);
PrintStream out = System.out;
int n = in.nextInt();
int k1 = in.nextInt();
Stack s1 = new Stack();
Stack s2 = new Stack();
int[] a1 = new int[k1];
for(int i=0;i<k1;i++)
a1[i] = in.nextInt();
int k2 = in.nextInt();
int[] a2 = new int[k2];
for(int i=0;i<k2;i++){
a2[i] = in.nextInt();
}
for(int i=k1-1;i>=0;i--)
s1.push(a1[i]);
for(int i=k2-1;i>=0;i--)
{s2.push(a2[i]);
}
int Maxtime = 10000;
int count = 0;
boolean flag = false;
while(! s1.isEmpty() && ! s2.isEmpty() && Maxtime>0){
int p1 = (Integer) s1.pop();
int p2 = (Integer) s2.pop();
if(p1<p2){
int[] a3 = new int[s2.size()+2]; for(int i=0;i<a3.length-2;i++)
a3[i] = (Integer) s2.pop();
a3[a3.length-2] = p1;
a3[a3.length-1] = p2;// max
for(int i= a3.length-1;i>=0;i--)
{
s2.add(a3[i]);
}
count++;
}else{
int[] a4 = new int[s1.size()+2]; for(int i=0;i<a4.length-2;i++)
a4[i] = (Integer) s1.pop();
a4[a4.length-2] = p2;
a4[a4.length-1] = p1; // max
for(int i= a4.length-1;i>=0;i--)
s1.add(a4[i]);
count++;
}
Maxtime--; }
if(Maxtime==0)
System.out.println(-1);
else if(s1.isEmpty())
System.out.println(count+" "+2);
else
out.println(count+" "+ 1);
}
public static void main(String[] args){
// run(); run1();
}
}

Python 程序

def C546():

     n = int(raw_input())

     l=lambda:map(int,raw_input().split())

     a = l()[1:]

     b = l()[1:]

     c = 0

     R = set()

     while a and b:

         c +=1

         A = a.pop(0)

         B = b.pop(0)

         if A > B:

             a+=[B,A]

         else:

             b+=[A,B]

         r =(tuple(a),tuple(b))

         if r in R :

             print -1

             exit(0)

         R.add(r)

     print c, 1 if a else 2

    
if __name__=='__main__':

    #A546()

    #B546()

    C546()

546C. Soldier and Cards的更多相关文章

  1. cf 546C Soldier and Cards

    题目链接:C. Soldier and Cards Two bored soldiers are playing card war. Their card deck consists of exact ...

  2. 【CodeForces - 546C】Soldier and Cards (vector或队列)

    Soldier and Cards 老样子,直接上国语吧  Descriptions: 两个人打牌,从自己的手牌中抽出最上面的一张比较大小,大的一方可以拿对方的手牌以及自己打掉的手牌重新作为自己的牌, ...

  3. 【codeforces 546C】Soldier and Cards

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 队列 Soldier and Cards

    Soldier and Cards 题目: Description Two bored soldiers are playing card war. Their card deck consists ...

  5. CF Soldier and Cards (模拟)

    Soldier and Cards time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  6. Codeforces Round #304 (Div. 2) C. Soldier and Cards 水题

    C. Soldier and Cards Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/546 ...

  7. Codeforces Round #304 (Div. 2) C. Soldier and Cards —— 模拟题,队列

    题目链接:http://codeforces.com/problemset/problem/546/C 题解: 用两个队列模拟过程就可以了. 特殊的地方是:1.如果等大,那么两张牌都丢弃 : 2.如果 ...

  8. C - Soldier and Cards

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Two bo ...

  9. queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards

    题目传送门 /* 题意:两堆牌,每次拿出上面的牌做比较,大的一方收走两张牌,直到一方没有牌 queue容器:模拟上述过程,当次数达到最大值时判断为-1 */ #include <cstdio&g ...

随机推荐

  1. width100%,设置padding或border溢出解决方法

    .box { width: 100px; height: 100px; background: red;} .bd { width: 100%; padding: 10px; background: ...

  2. MyEclipse中新建html5中文乱码

    近期刚开始入门html5 和 javascript 用MyEclipse2104创建html5时,遇到浏览器显示中文乱码的问题 [MyEclipse中设置的html5,jsp编码都采用的UTF-8] ...

  3. mina socket底层主流程源码实现

    一,mina的架构 mina 架构可以大致分为三部分,ioService ,ioFilterChain , IoHandler   ioService:用于接受服务或者连接服务,例如socket 接收 ...

  4. centos问题集锦

    一. 为什么新装的centos系统无法使用xshell,putty等工具连接? 原因:sshd服务没有启动. 解决: 1)使用命令rpm -qa | grep ssh查看是否已经安装了ssh 2)使用 ...

  5. html设置360兼容/极速模式

    由于众所周知的情况,国内的主流浏览器都是双核浏览器:基于Webkit内核用于常用网站的高速浏览.基于IE的内核用于兼容网银.旧版网站.以360的几款浏览器为例,我们优先通过Webkit内核渲染主流的网 ...

  6. php随机验证码

    今天同学问我,用php怎么写验证码,由于是新手所以花了半天的时间才完成.而且功能很是简单呵呵.今天本来打算写session和cookie的看来是要明天了. <?php $image_width= ...

  7. selenium+python 浏览器标签页跳转 switch_to_window

    浏览器页面跳转方法记录: from selenium import webdriver import time browser = webdriver.Chrome() first_url='http ...

  8. 每日一“酷”之Cookie

    Cookie---Http Cookie 作用:Cookie模块定义一些类来解析和创建HTTP cookie首部 Cookie模块为大多数符合RFC 2109的cookie实现一个解析器.这个实现没有 ...

  9. 浅谈 WPF布局

    我们首先来了解一下图形化用户界面(Graphic User Interface)也就是我们常常听到的GUI.举个简单的例子,同样是数据,我们可以用控制台程序加格式控制符等输出,但是这些都不如GUI来的 ...

  10. Teradata 的rank() 和 row_number() 函数

    Teradata数据库中也有和oracle类似的分析函数,功能基本一样.示例如下: RANK() 函数   SELECT * FROM salestbl ORDER BY 1,2; storeid p ...