java作业:随机出现一张java扑克牌随机发牌。包括花色和点数。【不算大王和小王】怎么写

&nbsp&nbsp &
&nbsp&nbsp
&nbsp&nbsp &
&nbsp&nbsp网站地图 &nbsp&nbsp &
&nbsp&nbsp
多人在线扑克游戏源码带服务端完整版,本项目源码是一套网络版的扑克牌项目源码,带服务器端源码,服务器端也是用java做的,打开游戏以后需要配置IP服务器端的IP和端口,服务端默认监听9999端口,客户端ip填写10.0.2.2,端口信息不用管直接点连接就可以连接到电脑上的服务端。项目源码注释比较丰富,可以研究一下里面的算法之类的。搭建这个服务端环境废了我半天劲,不知道是不是我运行的方法不对,打开前两个游戏客户端没有问题,打开第三个就开始直接强制退出。游戏没玩成所以没有截那部分的
importjava.util.ArrayLimportjava.util.Cimportjava.util.HashMimportjava.util.HashSimportjava.util.Limportjava.util.Mimportjava.util.R/***(c)2010华润(集团)有限公司版权所有.保留所有权利.**文件名称:Chains.java*程序说明:*创建日期:Jun18,2010*
经常遇到这个问题,自己做了一下,随机发52张牌,分给4个人,每个人手中的牌是13张无重复,花色S代表黑桃,H代表红桃,D代表方块,C代表梅花。发完牌后4组牌由小到大排序(依数字的大小从1-13)。若有同一数字牌出现则依照S、H、D、C顺序。1.Puke.javapackagepack3;importjava.util.*;publicclassPuke{intpoke[]=newint[52];intpoke_a1[]=newint[13];intpoke_a2[]=newi
最近看到javaEye上有一位仁兄帖出来取扑克牌乱序算法。看过还是有问题,都没有人想到而纠正过来,本想直接回复的,可以已经结帖了。我又最近比较空,所以自己博客里也写写,算是给一些新手和老手一点新的算法思路。packagecom.importjava.util.Eimportjava.util.H/***乱序扑克牌洗牌方法**@authorvirture**/publicclassCards{HashtablehtMember
packagecom.tengfei.lesson06;&&&importjava.util.V&&importjava.util.LinkedL&&importjava.util.R&&importjava.util.ListI&&&publicclassDealCards{&&&
扑克发牌算法是棋牌游戏中常用的基础算法,也是游戏开发人员需要熟悉的基础算法之一。下面介绍一下该算法的一种实现方式。首先给扑克牌中每张牌设定一个编号,下面算法实现的编号规则如下:u红桃按照从小到大依次为:1-13u方块按照从小到大依次为:14-26u黑桃按照从小到大依次为:27-39u梅花按照从小到大依次为:40-52u小王为53,大王为54算法实现如下:u首先按照以上编号规则初始化一个包含108个数字的数组u每次随机从该数组中抽取一个数字,分配给保存玩家数据的数组实现该功能的
前一段时间找实习,腾讯面试中一轮面试官被问到这个题目,我回答了下面解法中的第一种,太搓了,直接遭面试官鄙视了,回来搜了搜,发现一种更好的解法(下面解法中的第二种),今天偶尔发现解法2其实有毛病,于是改进了,有了算法3和算法4.前提:一副扑克牌有54张,因此我们可以一个整型数组array[54]或者map来存储,&A&用0~3,&2&用4~7,&3&用8~11......&K&用48~51,小鬼用5
程序中常常会用到常量值来定义一些相对固定的有实际意义值。比如,你要定义一个扑克牌的类,扑克牌有花色和数字两种属性,然而花色只有红桃(红心)、方块、黑梅、黑桃四种,这时你就可以定义四个常量分别表示这四种花色,这样定义的好处是:每次给花色赋值时只用到常量值的名称就可以,不易出错;如果某一天扑克出现新玩法需增加一个花色,只有增加一个常量值就可以,扩展性强;Java中常量的定义Java中常量的定义,最常见的就是以下形式:publicstaticfinalT[CONSTNAME]=[V
小明刚上小学,学会了第一个扑克牌“魔术”,到处给人表演。魔术的内容是这样的:他手里握着一叠扑克牌:A,2,....J,Q,K一共13张。他先自己精心设计它们的顺序,然后正面朝下拿着,开始表演。只见他先从最下面拿一张放到最上面,再从最下面拿一张翻开放桌子上,是A;然后再从最下面拿一张放到最上面,再从最下面拿一张翻开放桌子上,是2;......如此循环直到手中只有一张牌,翻开放桌子上,刚好是K。这时,桌上牌的顺序是:A,2,3,4,5,6,7,8,9,10,J,Q,K请你计算一下
趁着过年这段时间,我将算法导论这本书看了一遍,感觉受益匪浅。着这里也根据算法导论中所涉及到的算法用java实现了一遍。第一篇我们就从排序开始,插入排序的原理很简单,就像我们玩扑克牌时一样。如果手里拿的牌比他前一张小,就继续向前比较,知道这张牌比他前面的牌打时候就可以插在他的后面。当然在计算机中我们相应的也需要将对比过的牌向后移一位才可以。这里直接给出算法,相信很多程序员都感觉有些程序比我们的自然语言都要好理解。publicclassSort{publicvoidsort(in
一.概念&&&&&&&基数排序也不是基于比较和元素移位的,又称桶子法;数据结构课本上首先由扑克牌的排序引入,继而引出多关键字比较。&&&&&&&本文是基于计数排序的基数排序,只介绍最低位优先(LeastSignificantDigitFirst),谷歌之发现就几乎没有介绍MSD的,所谓LSD就是从数字的最低位逐个比较,比较的趟数就是最大数
罗大佑有歌云:“无聊的日子总是会写点无聊的歌曲......”,我不是歌手,我是程序员,于是无聊的日子总是会写点无聊的程序。程序不能太大,不然没有时间完成;程序应该有趣,不然就达不到消磨时间的目的;程序应该有那么一点挑战性,不然即使写完了也没有进步。金钩钓鱼游戏是我儿时经常玩的一种扑克牌游戏,规则非常简单,两个玩家,一旦牌发到手里之后,接下来每个人出什么牌基本上已经就定了,玩家没有自己做决策的机会,所以这个游戏很容易用程序自动模拟出来。(一)关于金钩钓鱼游戏基本规则(简化版):
&摘要:A.扑克发牌算法是棋牌游戏中常用的基础算法,也是游戏开发人员需要熟悉的基础算法之一。下面介绍一下该算法的一种实现方式。首先给扑克牌中每张牌设定一个编号,下面算法实现的编号规则如下:u红桃按照从小到大依次为:1-13u方块按照从小到大依次为:14-26u黑桃按照从小到大依次为:27-39u梅花按照从小到大依次为:40-52u小王为53,大王为54算法实现如下:u首先按照以上编号规则初始化一个包含108个数字的数组u每次随机从该数组中抽取一个数字,分配给保存玩家
2012年,哪些开发商的收入最多?AppAnnie整理了2012年全年的收入数据,并以扑克牌的方式展现给各位,作为您的参考。RevenueTop52的开发商,找到你了么?2个中国开发商、11个日本开发商、3个韩国开发商、23个美国开发商......&&&来源:appannie&
你可能还喜欢
你可能感兴趣
阿里云教程中心为您免费提供信息,所有相关内容均不代表阿里云的意见!投稿删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内答复
售前咨询热线
服务与支持
账号与支持
关注阿里云
International\ 经验分享
写的简单的java第三季的课后作业
1、card类,继承了comparable接口
package cardGame_
public class card implements Comparable&card&{
private String card_
private String card_
private double card_
public card(){}
* 创建一般牌*/
public card(String card_num,String card_type) throws CardsException{
//生成卡牌的数值
int tempInt = Integer.parseInt(card_num);
if(tempInt&1
tempInt&13)
throw new CardsException("输入了不合法的卡片数值,卡片数值应该在1~13之间");
switch(tempInt){
case 11:this.card_num="J";
case 12:this.card_num="Q";
case 13:this.card_num="K";
default:this.card_num=tempInt+"";
if(card_type.equals("红桃")){
this.card_type = card_
tempNum = 0;
} else if(card_type.equals("黑桃")){
this.card_type = card_
tempNum = 13;
} else if(card_type.equals("梅花")){
this.card_type = card_
tempNum = 26;
} else if(card_type.equals("方块")){
this.card_type = card_
tempNum = 39;
else throw new CardsException("不合法的花色,本构造器不允许大王小王");
this.card_indent = tempInt+tempN
public String getCard_num() {
return card_
public void setCard_num(String card_num) {
this.card_num = card_
public String getCard_type() {
return card_
public void setCard_type(String card_type) {
this.card_type = card_
public double getCard_indent() {
return card_
public void setCard_indent(double card_indent) {
this.card_indent = card_
public int hashCode() {
final int prime = 31;
int result = 1;
temp = Double.doubleToLongBits(card_indent);
result = prime * result + (int) (temp ^ (temp &&& 32));
public boolean equals(Object obj) {
if (this == obj)
if (obj == null)
if (getClass() != obj.getClass())
card other = (card)
if (Double.doubleToLongBits(card_indent) != Double.doubleToLongBits(other.card_indent))
public int compareTo(card card) {
if(this.card_indent&card.card_indent) return 1;
else if(this.card_indent&card.card_indent) return -1;
public String toString() {
return " "+card_type + card_num+" " ;
2、实践一下自定义异常类,并且在上文的card类的构造方法上抛出
package cardGame_
public class CardsException extends Exception{
public CardsException(){}
public CardsException(String message){super();}
创建的player类,countScore()方法是后面发现要比较大小凑得..
package cardGame_
import java.util.ArrayL
import java.util.L
public class player {
private List&card& deliverdCard = new ArrayList&card&();
public player(String name){
this.name =
public String getName() {
public List&card& getDeliverdCard() {
return deliverdC
public void pushCard(card card){
deliverdCard.add(card);
public void pushCards(List&card& cards){
deliverdCard.addAll(cards);
public double countScore(){
return deliverdCard.get(deliverdCard.size()-1).getCard_indent()+
deliverdCard.get(deliverdCard.size()-2).getCard_indent();
public String toString() {
package cardGame_
import java.util.ArrayL
import java.util.L
import java.util.S
public class cardGame{
public static int ALLNUM = 13*4;
private List&card& RandomC
private List&card& BasicCardS
private List&player& playerL
public cardGame(){
public void newRound(Scanner console) throws CardsException{
genaratePlayer(console);
genarateCards();
while(true){
deliverCard(console);
compareCard();
System.out.println("是否继续:true/false");
String temp = console.next();
if(temp.equals("true"))
//生成玩家
private void genaratePlayer(Scanner console){
playerList = new ArrayList&player&();
System.out.println("现在开始加入玩家(建议加入两个以上玩家)");
while(true){
System.out.println("请输入玩家姓名:");
String temp = console.next();
player tempP = new player(temp);
playerList.add(tempP);
System.out.println("是否继续:true/false");
temp = console.next();
if(temp.equals("true"))
//加入空缺的玩家,以robot代替
int j = 1;
while(playerList.size()&2){
player tempRob = new player("robot"+j);
playerList.add(tempRob);
System.out.println("加入玩家完毕!\n");
printPlayerList();
//发牌,如果牌池无牌了则停止
private void deliverCard(Scanner console) throws CardsException{
if(RandomCards.size()&=playerList.size()*2) isRestartRound(console);
for (player temp : playerList) {
temp.pushCard(RandomCards.get(0));
temp.pushCard(RandomCards.get(1));
RandomCards.remove(0);
RandomCards.remove(1);
//决定胜负
private void compareCard(){
System.out.println("现在公布手牌");
player MaxOne = playerList.get(1);
for (player tempP:playerList) {
if(tempP.countScore()&MaxOne.countScore()) MaxOne = tempP;
System.out.println(tempP.toString()+":");
printCardsList(tempP.getDeliverdCard(), ALLNUM);
System.out.println("赢家是:"+MaxOne.toString());
private void isRestartRound(Scanner console) throws CardsException {
System.out.println("牌池已经干枯,是否开始新的一局游戏?");
String tempStr = console.next();
if(tempStr.equals("true")) this.newRound(console);
else System.exit(0);
private void genarateCards() throws CardsException{
BasicCardSet = new ArrayList&card&();
RandomCards = new ArrayList&card&();
for(int i=1 ; i&=13 ;i++){
BasicCardSet.add(new card(i+"","红桃"));
BasicCardSet.add(new card(i+"","黑桃"));
BasicCardSet.add(new card(i+"","梅花"));
BasicCardSet.add(new card(i+"","方块"));
BasicCardSet.sort(null);
System.out.println("现有牌组如下\n");
printCardsList(BasicCardSet,ALLNUM/4);
System.out.println("洗牌");
//生成随机的卡片组合RandomCards
int randomI
for(int i=1 ;i&=ALLNUM; i++){
randomInt = (int)Math.floor(ALLNUM*Math.random());//使用floor来避免数组越界的异常出现
temp = BasicCardSet.get(randomInt);
}while(RandomCards.contains(temp));
RandomCards.add(temp);
private void printPlayerList(){
System.out.println("现有以下玩家");
StringBuilder tempStr = new StringBuilder();
for (player temp : playerList) tempStr.append(temp.toString()+"\n");
System.out.println(tempStr.toString());
private void printCardsList(List&card& cards,int lineWidth){
StringBuilder tempStr = new StringBuilder();
//通过i控制每行打印多少卡片
for (card card : cards){
if(i&lineWidth){
tempStr.append("\n");
tempStr.append(card.toString());
System.out.println(tempStr.toString());
public static void main(String[] args) throws CardsException {
Scanner console = new Scanner(System.in);
cardGame new1 = new cardGame();
new1.newRound(console);
若觉得本文不错,就分享一下吧!
请登录后,发表评论
评论加载中...
Copyright (C) 2018 imooc.com All Rights Reserved | 京ICP备 号-2java作业:随机出现一张扑克牌。包括花色和点数。【不算大王和小王】怎么写 一副扑克牌52张不含大王小王,抽一张可能抽到( )种花...
你正在浏览: & >
java作业:随机出现一张扑克牌。包括花色和点数。【不算大王和小王】怎么写
ja作业:随机出现一张扑克牌。包括花色和点数。【不算大王和小王】怎么写
ja代码随机出现一张扑克牌。包括花色和点数。【不算大王和小王】
生成1-13的随机数String[] s = new String[] { &红心&;, breed=&ppublic class PokerTest {public static void main(String[] args) {Poker. + breed + &]&quot.println(p, &黑桃&.random() * 4);p.show();}}class Poker {public Sint j = (int) (Math.toString()), &黑花& }.breed = s[j];Spublic Poker() {}public static void show() {int i = (int) (Math.random() * 13) + 1;//Poker [num=& + num + &quot, &方块&.num =Poker p = new Poker();}@Overridepublic String toString() {return &quot
生成1-13的随机数String[] s = new String[] { &红心&;, breed=&ppublic class PokerTest {public static void main(String[] args) {Poker. + breed + &]&quot.println(p, &黑桃&.random() * 4);p.show();}}class Poker {public Sint j = (int) (Math.toString()), &黑花& }.breed = s[j];Spublic Poker() {}public static void show() {int i = (int) (Math.random() * 13) + 1;//Poker [num=& + num + &quot, &方块&.num =Poker p = new Poker();}@Overridepublic String toString() {return &quot
取2个随机数,一个与4求余获得花色,一个与13求余获得点数
二维数组?
你可能感兴趣的内容?//“输入每手牌可能是个子,对子,顺子(连续5张),三个,炸弹(四个)和对王中的一种,
//不存在其他情况,由输入保证两手牌都是合法的,顺子已经从小到大排列“
//这句规则让牌面类型的确定和大小的比较直接可以转换为牌个数的比较了,这是解决测试问题的捷径!
//所以一定要善于利用题目已知条件!!!
#include &iostream&
#include &string&
#include &algorithm&
int main(){
while(getline(cin,line)){
if(line.find(&joker JOKER&)!=-1)
cout&&&joker JOKER&&&
int dash=line.find('-');
string car1=line.substr(0,dash);
string car2=line.substr(dash+1);
int c1=count(car1.begin(),car1.end(),' ');
int c2=count(car2.begin(),car2.end(),' ');
string first1=car1.substr(0,car1.find(' '));
string first2=car2.substr(0,car2.find(' '));
string str=&JQKA2jokerJOKER&;
if(c1==c2){
if(str.find(first1)&str.find(first2))
cout&&car1&&
cout&&car2&&
cout&&car1&&
else if(c2==3)
cout&&car2&&
cout&&&ERROR&&&
import java.util.ArrayL
import java.util.S
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] left,
String nextLine,outS
while(sc.hasNext()){
nextLine = sc.nextLine();
//有王炸就王炸最大
if(nextLine.contains(&joker JOKER&)){
outString = &joker JOKER&;
//拆分 先拆成左右 再拆成单排
line = nextLine.split(&-&);
left = line[0].split(& &);
right = line[1].split(& &);
//炸弹最大
if(left.length == 4 && right.length != 4){
outString = line[0];
}else if(right.length == 4 && left.length != 4){
outString = line[1];
// 牌数相同的情况下比较最小的牌的大小,compare方法返回牌所对应的值
else if(right.length == left.length){
if(count(left[0])&count(right[0])){
outString = line[0];
outString = line[1];
outString = &ERROR&;
System.out.println(outString);
//2-JOKER 按大小返回2-16
private static int count(String str) {
return &JQKA2jokerJOKER&.indexOf(str);
#include &stdio.h&
#include &stdlib.h&
#include &string.h&
int *infoCard(char *a)
int *info = (int*)calloc(2, sizeof(int));
if(*a == 'j'){
info[0] = 6;
switch(*a){
case '1': info[1] = 10;
case 'A': info[1] = 14;
case '2': info[1] = 15;
case 'J': info[1] = 11;
case 'Q': info[1] = 12;
case 'K': info[1] = 13;
default: info[1] = *a-'0';
if(*(a++) == ' ')
++info[0];
++info[0];
int main(int argc, char const *argv[])
char a[64], b[64];
scanf(&%[^-]-%[^\n]&, a, b);
int *ai = infoCard(a), *bi = infoCard(b);
if(ai[0] == 6)
printf(&%s\n&, a);
else if(bi[0] == 6)
printf(&%s\n&, b);
else if(ai[0] == bi[0])
printf(&%s\n&, ai[1] & bi[1] ? a : b);
else if(ai[0] == 4)
printf(&%s\n&, a);
else if(bi[0] == 4)
printf(&%s\n&, b);
printf(&Error!\n&);
#include &string&
#include &vector&
#include &iostream&
#define PrintS1 {cout && s1 &&return 0;}
#define PrintS2 {cout && s2 &&return 0;}
int count(string & str){
int n = 0;
string::size_type
i = 0 , k=0;
while ((k=str.find(' ',i)) != string::npos){
n++;i = k + 1;
int main(){
getline(cin, input);
string s1, s2;
int t = input.find('-');
s1 = input.substr(0, t);
s2 = input.substr(t + 1);
int n1 = count(s1);
int n2 = count(s2);
string POKER = &JQKA2jokerJOKER&;
vector&string& MAX = { &joker JOKER&, &JOKER joker& };
if (s1 == MAX[0] || s1 == MAX[1]) PrintS1
if (s2 == MAX[0] || s2 == MAX[1]) PrintS2
if (n1==n2){
string f1, f2;
string::size_type k = s1.find(' ');
f1 = s1.substr(0, k);
k = s2.find(' ');
f2 = s2.substr(0, k);
if (POKER.find(f1) & POKER.find(f2))PrintS1
if (n1 == 3 && n2 != 3) PrintS1
if (n2 == 3 && n1 != 3) PrintS2
cout && &ERROR& &&
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
while(scanner.hasNext()){
String A=scanner.nextLine();
System.out.println(new Main().judge2(A));
scanner.close();
//得到牌的张数
public int judge(String a){
String[] A=a.split(& &);
if(A.length==1)
else if(A.length==3)
else if(A.length==4)
else if(A.length==5)
else return 2;
//得到输出结果的方法
public String judge2(String A){
String[] op=A.split(&-&);
int a=judge(op[0]);
int b=judge(op[1]);
if(op[0].equals(&joker JOKER&)||op[1].equals(&joker JOKER&))
return &joker JOKER&;
return op[0];
else if(b==4)
return op[1];
else return &ERROR&;
String a1=op[0].split(& &)[0];
String a2=op[1].split(& &)[0];
return lk(a1,a2)?op[0]:op[1];
//判断字符串A是否大于字符串b
public boolean lk(String a,String b){
String[] s=new String[]{&3&,&4&,&5&,&6&,&7&,&8&,&9&,&10&,&J&,&Q&,&K&,&A&,&2&};
int c=0,d=0;
for(int i=0;i&s.i++){
if(s[i].equals(a))
if(s[i].equals(b))
return c&=d?true:
#include&iostream&
#include&vector&
#include&string.h&
char firstCards[15],secCards[15];
int oneChar(char token)
switch (token)
return 11;
return 12;
return 13;
return 14;
return 15;
return atoi(&token);
int Card(int *cardnum,char *cardStr)
int numcount=0;
for(char *token=strtok(cardStr,& &);token!=token=strtok(nullptr,& &))
charsize=strlen(token);
if (charsize==1)
cardnum[numcount]=oneChar(*token);
}else if (charsize==2)
cardnum[numcount]=10;
}else if (charsize==5)
if(strcmp(token,&JOKER&))
cardnum[numcount]=100;
cardnum[numcount]=200;
void sameType(int firstCardNum,int secCardNum,int type)
if (firstCardNum&=secCardNum)
cout&&firstCards&&
cout&&secCards&&
void diffType(int firstCardNum,int secCardNum,int firstType,int secType)
if(firstCardNum==100)
cout&&firstCards&&
else if (secCardNum==100)
cout&&secCards&&
else if(firstType==4)
cout&&firstCards&&
else if(secType==4)
cout&&secCards&&
cout&&&ERROR&&&
int main()
char twoCard[100]={0};
while (cin.getline(twoCard,sizeof(twoCard)))
char *firstCardstr=strtok(twoCard,&-&);
char *secCardstr=strtok(nullptr,&-&);
strcpy(firstCards,firstCardstr);
strcpy(secCards,secCardstr);
int firstCardNum[5]={0},secCardNum[5]={0};
int firstCardtype=0,secCardtype=0;
firstCardtype=Card(firstCardNum,firstCardstr);
secCardtype=Card(secCardNum,secCardstr);
if(firstCardtype==secCardtype)
sameType(*firstCardNum,*secCardNum,firstCardtype);
diffType(*firstCardNum,*secCardNum,firstCardtype,secCardtype);
memset(firstCards,0,sizeof(firstCards));
memset(secCards,0,sizeof(secCards));
import java.util.HashM
import java.util.M
import java.util.S
public class Main{
public static void main(String[] args) {
Map&String,Integer& map = new HashMap&String,Integer&();
for(int i = 3;i&11;i++)
map.put(&&+i, i);
map.put(&J&, 11);
map.put(&Q&, 12);
map.put(&K&, 13);
map.put(&A&, 14);
map.put(&2&, 15);
map.put(&joker&, 16);
map.put(&JOKER&, 17);
Scanner in = new Scanner(System.in);
while(in.hasNext()){
String[] s = in.nextLine().split(&-&);
String[] l = s[0].split(& &);
String[] r = s[1].split(& &);
int type1 = l.
int type2 = r.
switch (type1) {
if(type2==1)
System.out.println(map.get(l[0])&map.get(r[0])?s[0]:s[1] );
else if(type2==2){
if(isTwoJoker(r))
System.out.println(s[1]);
System.out.println(&ERROR&);
}else if(type2==4){
System.out.println(s[1]);
System.out.println(&ERROR&);
if(type2==2){
if(isTwoJoker(l))
System.out.println(s[0]);
else if(isTwoJoker(r)){
System.out.println(s[1]);
System.out.println(map.get(l[0])&map.get(r[0])?s[0]:s[1]);
}else if(type2==4){
if(isTwoJoker(l))
System.out.println(s[0]);
System.out.println(s[1]);
System.out.println(&ERROR&);
if(type2==2){
if(isTwoJoker(r))
System.out.println(s[1]);
}else if(type2==3){
System.out.println(map.get(l[0])&map.get(r[0])?s[0]:s[1]);
}else if(type2==4){
System.out.println(s[1]);
System.out.println(&ERROR&);
if(type2==2){
if(isTwoJoker(r))
System.out.println(s[1]);
}else if(type2==4){
System.out.println(map.get(l[0])&map.get(r[0])?s[0]:s[1]);
System.out.println(&ERROR&);
if(type2==5){
if(isTwoJoker(r))
System.out.println(s[1]);
System.out.println(map.get(l[0])&map.get(r[0])?s[0]:s[1]);
}else if(type2==4){
System.out.println(s[1]);
System.out.println(&ERROR&);
public static boolean isTwoJoker(String[] s){
if(s.length!=2)
if(s[0].equals(&joker&)&&s[1].equals(&JOKER&))
你的代码仅仅处理了一个测试用例,没有循环处理多个测试用例。?这是什么鬼
还特意改成错的代码 通过70% 对了就说只处理一个用例
#include &iostream&
#include &string&
int main()
string cards=&JQKA2jokerJOKER&;
while(getline(cin,poker)){
string opp1,opp2;
opp1=poker.substr(0,poker.find('-'));
opp2=poker.substr(poker.find('-')+1);
intlen1=opp1.size();
intlen2=opp2.size();
if(opp1==&joker JOKER&||opp2==&joker JOKER&) cout&&&joker JOKER&&&
elseif((len1==7||len1==11)&&(len2!=7&&len2!=11)) cout&&opp1&&
elseif((len1!=7&&len1!=11)&&(len2==7||len2==11)) cout&&opp2&&
elseif((len1==7||len1==11)&&(len2==7||len2==11))
cout&&(cards.find(opp1[0])&cards.find(opp2[0])?opp1:opp2)&&
elseif((len1==9||len1==10)&&(len2==9||len2==10))
cout&&(cards.find(opp1[0])&cards.find(opp2[0])?opp1:opp2)&&
elseif((len1==5||len1==8)&&opp1!=&joker&&&opp1!=&JOKER&
&&(len2==5||len2==8)&&opp2!=&joker&&&opp2!=&JOKER&)
cout&&(cards.find(opp1[0])&cards.find(opp2[0])?opp1:opp2)&&
elseif((len1==3||len1==5)&&opp1!=&joker&&&opp1!=&JOKER&
&&(len2==3||len2==5)&&opp2!=&joker&&&opp2!=&JOKER&)
cout&&(cards.find(opp1[0])&cards.find(opp2[0])?opp1:opp2)&&
elseif(((len1==1||len1==2)&&(len2==1||len2==2))||(len1==1&&len2==5)
||(len1==5&&len2==1)||(len1==5&&len2==5))
cout&&(cards.find(opp1)&cards.find(opp2)?opp1:opp2)&&
cout&&&ERROR&&&
这道题的测试用例有问题,会出现1
,还会出现23456这种顺子,严重不合理和误导读者,华为OJ平台上不会出现这种情况,请管理员重视,麻烦回复一下!!!!
#include &iostream&
#include &string&
#include &vector&
#include &algorithm&
int main()
string card = &JQKA2&;
vector&string& MAX;
MAX.push_back(&joker JOKER&);
MAX.push_back(&JOKER joker&);
while(getline(cin, input))
unsigned int pos = input.find('-');
int len =input.size();
string s1 = input.substr(0, pos);
string s2 = input.substr(pos+1, len-pos-1);
//cout&&s1&&& &&&s2&&
//这里有个坑就是 &10&是两位数,所以只能找空白
int n1 = count(s1.begin(),s1.end(),' ');
int n2 = count(s2.begin(),s2.end(),' ');
// double joker
if (s1 == MAX[0] || s1 == MAX[1])
{ cout&&s1&&}
if (s2 == MAX[0] || s2 == MAX[1])
{ cout&&s2&&}
//&10&是两位数,只需要取第一位1就可以
if(n1==n2)
int l = card.find(s1[0]);
int r = card.find(s2[0]);
string ans = l&r? s1:s2;
cout&&ans&&
if(n1==3 && n2!=3)
{ cout&&s1&&}
if(n1!=3 && n2==3)
{ cout&&s2&&}
cout&&&ERROR&&&
#include&string&
#include&iostream&
//定义单张牌面的大小顺序(在数组前面的小,后面的大)
string arr[] = { &3&, &4&, &5&,
&6&, &7&, &8&, &9&,
&10&, &J&, &Q&, &K&,
&A&, &2&, &joker&, &JOKER& };
//在main函数中通过牌面在数组中的下标比大小,该函数返回单张牌面在数组中的下标
int FindIndex(string str)
for (int i = 0; i & 15; i++)
if (arr[i] == str)
return 15;
int main()
int cont1 = 0, cont2 = 0;
string str1[5] = { && }, str2[5] = {
while ((ch = getchar()) != '-')//输入1手牌,并记录牌张数
if (ch == ' ')
str1[cont1] +=
while ((ch = getchar()) != '\n')//输入2手牌,并记录牌张数
if (ch == ' ')
str2[cont2] +=
//输出ERROR的情况:两手牌数目不等 且 两手牌中任何一手牌都不是四个或对王
if (cont1 != cont2 && (cont1 != 4 &&
cont2 != 4 && str1[0] != &joker& && str2[0]
!= &joker&))
cout && &ERROR& &&
if (cont1 == cont2)//数目相等比大小
if (FindIndex(str1[0]) & FindIndex(str2[0]))
else//数目不等,两手牌中至少有一个是四个或对王
if ((cont1 == 2 && str1[0] == &joker&)
|| (cont1 == 4 && !(cont2 == 2 && str2[0] ==
&joker&)))
if (is1max)//输出
for (int i = 0; i & cont1; i++)
cout && str1[i];
if (i & cont1 - 1) cout && & &;
for (int i = 0; i & cont2; i++)
cout && str2[i];
if (i & cont2 - 1) cout && & &;
A = '3 4 5 6 7 8 9 10 J Q K A 2 joker JOKER'.split(' ')
s = raw_input()
a, b = s.split('-')
p, q = a.split(' '), b.split(' ')
if len(q) != len(p):
for t in p, q:
if len(t) == 2 and t[0] == 'joker':
w = ' '.join(t)
elif len(t) == 4:
w = ' '.join(t)
w = 'ERROR'
elif A.index(q[0]) & A.index(p[0]):
#一楼正解, 只能是牌中的一种。
# -*- coding:utf-8 -*-
import sys
if __name__ == '__main__':
while True:
tp = sys.stdin.readline().strip()
if not tp:
if 'joker JOKER' in tp:
print 'joker JOKER'
strs = ['3','4','5','6','7','8','9','10','J','Q','K','A','2','joker','JOKER']
s1,s2 = tp.split('-')
ss1 = s1.split(' ')
ss2 = s2.split(' ')
len1 = len(ss1)
len2 = len(ss2)
if len1 ==4 and len2!=4:
elif len2==4 and len1!=4:
elif len1==len2:
ind1 = strs.index(ss1[0])
ind2 = strs.index(ss2[0])
if ind1&ind2:
elif ind1&ind2:
print 'ERROR'
#include &iostream&
#include &vector&
#include &string&
#include &map&
#include &algorithm&
#include &regex&
map&int,int&
void initMap(){
for(int i=3;i&=10;i++)
rate[i+48]=i;
rate[74]=11;
rate[81]=12;
rate[75]=13;
rate[65]=14;
rate[50]=15;
rate[48]=10;
string Judge(string left,string right){
if(left==&&||right==&&) return &&;
if(left==&joker JOKER&||right==&joker JOKER&) return &joker JOKER&;
vector&char&
vector&char&
for(int i=1;i&=left.size();i++)
if(left[i]==' '||i==left.size()){
lv.push_back(left[i-1]);
for(int i=1;i&=right.size();i++)
if(right[i]==' '||i==right.size()){
rv.push_back(right[i-1]);
if(lv.size()==rv.size()){
return rate[lv[0]]&rate[rv[0]]?left:
if(lv.size()==4&&rv.size()!=4){
}else if(lv.size()!=4&&rv.size()==4){
return &ERROR&;
int main(){
initMap();
getline(cin,input);
int f=input.find('-');
string left=input.substr(0,f);
string right=input.substr(f+1);
cout&&Judge(left,right);
解题思想:
1.无论两方任意一方有人拿到joker JOKER或JOKER joker都认为其是胜利方。
2.如果没有人拿到大小王,就对每个人的牌进行判断:
(1)两个人的牌数一样多,在扑克牌数组中进行位置查找,如果哪一付手牌的位置靠后说明其牌大,
(因为根据题目规则,两个人的牌只有个子,对子,顺子,三个,炸弹,和王这几种情况,所以越靠后说明牌越大)
否则说明牌小。
(2)两个人的牌数不一样多,其中有一个人的牌的空格数是3,说明他的牌是炸弹,例如4 4 4 4有三个空格
和其他牌可以比较的只有炸弹和王,又因为两个人的牌数不一样多,而且有一个人的牌是炸弹,说明炸弹是胜利
#include &string&
#include &vector&
#include &iostream&
#define PrintS1 {cout && s1 &&return 0;}
#define PrintS2 {cout && s2 &&return 0;}
int count(string & str)
int n = 0;
string::size_type
i = 0 , k=0;
//find函数的返回值是整数,假如字符串存在包含关系,其返回值必定不等于npos,但如果字符串不存在包含关系,那么返回值就一定是npos
while ((k=str.find(' ',i)) !=
string::npos)//npos是一个常数一般是指不存在的位置,
i = k + 1;
int main()
getline(cin,input);
string s1,s2;
int t = input.find('-');//查找'-'在input中的位置t
s1 = input.substr(0, t);//取s1为从0-t的子串
s2 = input.substr(t + 1);//取s2为从t+1之后的子串
int n1 = count(s1);//count函数返回该字符串敏感词有几张牌
int n2 = count(s2);
string POKER = &JQKA2jokerJOKER&;
vector&string& MAX;
MAX.push_back(&joker JOKER&);
MAX.push_back(&JOKER joker&);//最大的牌是MAX
if (s1 == MAX[0] || s1 == MAX[1]) PrintS1
if (s2 == MAX[0] || s2 == MAX[1]) PrintS2
if (n1==n2)
string f1, f2;
string::size_
k = s1.find(' ');
f1 = s1.substr(0, k);
k = s2.find(' ');
f2 = s2.substr(0, k);
if (POKER.find(f1) & POKER.find(f2)) PrintS1
if (n1 == 3 && n2 != 3) PrintS1
if (n2 == 3 && n1 != 3) PrintS2
cout && &ERROR& &&
import java.util.HashM
import java.util.S
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
HashMap&String, Integer& map = new HashMap&String, Integer&();
map.put(&3&, 0);
map.put(&4&, 1);
map.put(&5&, 2);
map.put(&6&, 3);
map.put(&7&, 4);
map.put(&8&, 5);
map.put(&9&, 6);
map.put(&10&, 7);
map.put(&J&, 8);
map.put(&Q&, 9);
map.put(&K&, 10);
map.put(&A&, 11);
map.put(&2&, 12);
while (sc.hasNext()){
String str = sc.nextLine();
String left = str.split(&-&)[0];
String right = str.split(&-&)[1];
if(left.equals(&joker JOKER&) || right.equals(&joker JOKER&)){
System.out.println(&joker JOKER&);
} else if(left.length() == 7 && right.length() != 7){
System.out.println(left);
} else if(left.length() != 7 && right.length() == 7){
System.out.println(right);
} else if(map.get(left.charAt(0)+&&) & map.get(right.charAt(0)+&&)){
System.out.println(left);
System.out.println(right);
您的代码已保存
请检查是否存在数组越界非法访问等情况
case通过率为50.00%
求解答为啥有的测试用例会数组越界?
#include&iostream&
#include&cstdio&
#include&cstring&
#include&algorithm&
//类型 个子 0 对子 1 三子 2 炸弹 3 顺子 4 王炸 也属于炸弹的范畴
//各类型大小 3-17 3 - joker
struct poker{
char real[20];
void init(char* cards){
strcpy(real, cards);
size_t len = strlen(cards);
if(cards[0] == 'j' || cards[0] == 'J'){
//说明是王
if(len == 5){
value = cards[0] == 'j' ? 16 : 17;
value = 17;
if(cards[0] == '1'){
//说明10开始
switch(len){
case 2:type = 0;
case 5:type = 1;
case 8:type = 2;
case 11:type = 3;
case 10:type = 4;
value = 10;
switch(len){
case 1:type = 0;
case 3:type = 1;
case 5:type = 2;
case 7:type = 3;
default:type = 4;
switch(cards[0]){
case 'J':value = 11;
case 'Q':value = 12;
case 'K':value = 13;
case 'A':value = 14;
case '2':value = 15;
default:value = cards[0]-'0';
if(type == 3){
value += 20;
int main(){
char str[30];
struct poker poker1,poker2;
while(gets(str)){
size_t len = strlen(str);
for(int i = 0;i&i++){
if(str[i] == '-'){
str[i] = 0;
poker1.init(str);
poker2.init(&str[i+1]);
if(poker1.type == poker2.type || poker1.type == 3 ||
poker2.type == 3){
if(poker1.value & poker2.value){
printf(&%s\n&,poker1.real);
printf(&%s\n&,poker2.real);
puts(&ERROR&);
本题 需要对 ‘-’左右两边的字符串left_str,right_str分别判定为 个子,对子,顺子,三个,炸弹,还是 对王。
我的思路 是
这6种类型 它们的长度是不一样,直接通过长度来判断类型,然后计算每个类型的大小。
其中 要考虑“10”的出现,我分别讨论了“10”出现的各种情况。
这两点搞定,基本是没有什么问题了。
程序中我定义了结构体。
struct DataType
代码:写的长了
// main.cpp : Defines the entry point for the console application.
//#include &stdafx.h&
#include &iostream&
#include &string&
struct DataType
int Parase_help(char str)
if(str &= '9' && str &='3')
return int(str - '0');
else if(str == 'J')
return 11;
else if(str == 'Q')
return 12;
else if(str == 'K')
return 13;
else if(str == 'A')
return 14;
else if(str == '2')
void Parase_Each_Str(string &str, DataType &target)
//对10做特殊处理
if(str[0]=='1' && str[1] == '0')
if(str.size() == 2)
target.id = 1;
else if(str.size() == 5)
target.id = 2;
else if(str.size() == 8)
target.id = 4;
else if(str.size() == 11)
target.id = 5;
else if(str.size() == 10)
target.id = 3;
target.value = 10;
else if(str[0]=='j' && str[1]=='o' && str.size() == 5)
target.id = 1;
target.value = 16;
else if(str[0]=='J' && str[1]=='O' && str.size() == 5)
target.id = 1;
target.value = 17;
else if(str.size() == 11 && str[0]=='j')
target.id = 6;
else if(str.size() == 9 || str.size() == 12)
target.id = 3;
target.value = str[0]-'0';
else if(str.size() == 7)
target.id = 5;
target.value = str[0] - '0';
else if(str.size() == 5)
target.id = 4;
target.value = Parase_help(str[0]);
else if(str.size() == 3)
target.id = 2;
target.value = Parase_help(str[0]);
else if(str.size() == 1)
target.id = 1;
target.value = Parase_help(str[0]);
target.str =
void Parase(string &str, DataType &left, DataType &right)
string left_
string right_
for(int i=0; i&str.size(); ++i)
if(str[i] == '-')
left_str = str.substr(0,i);
right_str = str.substr(i+1,str.size()-i-1);
Parase_Each_Str(left_str,left);
Parase_Each_Str(right_str,right);
void output(DataType &left, DataType &right)
if(left.id == 6)
cout && left.str &&
else if(right.id == 6)
cout && right.str &&
else if(left.id == 5 && right.id &5)
cout && left.str &&
else if(right.id == 5 && left.id &5)
cout && right.str &&
else if(left.id == right.id)
if(left.value & right.value)
cout && left.str &&
cout && right.str &&
cout && &ERROR& &&
int main()
//freopen(&in.txt&, &r&, stdin);
while(getline(cin,str))
DataType left,
Parase(str,left,right);
output(left,right);
//fclose(stdin);
#include &iostream&
#include &string&
#include &vector&
#include &algorithm&
int main()
string card = &JQKA2&;
vector&string& MAX;
MAX.push_back(&joker JOKER&);
MAX.push_back(&JOKER joker&);
MAX.push_back(&joker&);
MAX.push_back(&JOKER&);
while (getline(cin,str)){
int t = str.find(&-&);
string LEFT = str.substr(0, t);
string RIGHT = str.substr(t + 1);
int LEFT_NUM = count(LEFT.begin(), LEFT.end(), ' ');
int RIGHT_NUM = count(RIGHT.begin(), RIGHT.end(), ' ');
/* 判断是否有对大小王 Start*/
if (LEFT == MAX[0] || LEFT == MAX[1])
cout && LEFT &&
if (RIGHT == MAX[0] || RIGHT == MAX[1])
cout && RIGHT &&
if (LEFT == MAX[2] || LEFT == MAX[3])
if (RIGHT_NUM == 0){
cout && LEFT &&
cout && &ERROR& &&
if (RIGHT == MAX[2] || RIGHT == MAX[3])
if (LEFT_NUM == 0){
cout && RIGHT &&
cout && &ERROR& &&
/* 判断是否有个、对大小王 END*/
就是10是两位的,所以查找的时候如果第一张牌是10,找的只是1
还好没有1*这样的其他数字了,所以这个方法还是可以的
//顺子(5张牌),对子(2张牌),三子(3张牌)
if (LEFT_NUM == RIGHT_NUM){//输入个数相同(中间的空格数量一样),就进行比较
if (LEFT_NUM &= 4 && RIGHT_NUM &= 4)//顺子
int LEFT_INDEX = card.find(LEFT[0]);//题目中已经说了已经排好序,所以就是直接比较第一位
int RIGHT_INDEX = card.find(RIGHT[0]);
string max = LEFT_INDEX & RIGHT_INDEX ? LEFT : RIGHT;
cout && max &&
if (LEFT_NUM == 2 && RIGHT_NUM == 2)//三子
int LEFT_INDEX_NUM = count(LEFT.begin(), LEFT.end(), LEFT[0]);
int RIGHT_INDEX_NUM = count(RIGHT.begin(), RIGHT.end(), RIGHT[0]);
if (LEFT_INDEX_NUM == 3 && LEFT_INDEX_NUM == 3)
int LEFT_INDEX = card.find(LEFT[0]);
int RIGHT_INDEX = card.find(RIGHT[0]);
string max = LEFT_INDEX & RIGHT_INDEX ? LEFT : RIGHT;
cout && max &&
cout && &ERROR& &&
if (LEFT_NUM == 1 && RIGHT_NUM == 1)//对子
int LEFT_INDEX_NUM = count(LEFT.begin(), LEFT.end(), LEFT[0]);
int RIGHT_INDEX_NUM = count(RIGHT.begin(), RIGHT.end(), RIGHT[0]);
if (LEFT_INDEX_NUM == 2 && LEFT_INDEX_NUM == 2)
int LEFT_INDEX = card.find(LEFT[0]);
int RIGHT_INDEX = card.find(RIGHT[0]);
string max = LEFT_INDEX & RIGHT_INDEX ? LEFT : RIGHT;
cout && max &&
cout && &ERROR& &&
if (LEFT_NUM == 0 && RIGHT_NUM == 0)//单个比对
int LEFT_INDEX = card.find(LEFT[0]);
int RIGHT_INDEX = card.find(RIGHT[0]);
string max = LEFT_INDEX & RIGHT_INDEX ? LEFT : RIGHT;
cout && max &&
//炸弹对比
if (LEFT_NUM == 3 && RIGHT_NUM != 3)
cout && LEFT &&
if (LEFT_NUM != 3 && RIGHT_NUM == 3)
cout && RIGHT &&
if (LEFT_NUM == 3 && RIGHT_NUM == 3)
int LEFT_INDEX_NUM = count(LEFT.begin(), LEFT.end(), LEFT[0]);
int RIGHT_INDEX_NUM = count(RIGHT.begin(), RIGHT.end(), RIGHT[0]);
if (LEFT_INDEX_NUM == 4 && RIGHT_INDEX_NUM == 4)
int LEFT_INDEX = card.find(LEFT[0]);
int RIGHT_INDEX = card.find(RIGHT[0]);
string max = LEFT_INDEX & RIGHT_INDEX ? LEFT : RIGHT;
cout && max &&
if (LEFT_INDEX_NUM == 4 && RIGHT_INDEX_NUM != 4)
cout && LEFT &&
if (LEFT_INDEX_NUM != 4 && RIGHT_INDEX_NUM == 4)
cout && RIGHT &&
cout && &ERROR& &&
#include &iostream&
#include &string&
#include &algorithm&
int getSpaceCount(string str){
//空格为0:一个牌;一个空格:俩个牌或双王;俩空格:三个牌;三空格:四个牌
int count = 0;
for (int i = 0; i & str.size(); i++){
if (str[i] == ' ')
int number(char c){
int num = 0;
switch (c){
case '1':num = 10;
case 'J': num = 11;
case 'Q':num = 12;
case 'K': num = 13;
case 'A': num = 14;
case '2': num = 15;
case 'j': num = 16;
num = c - '0';
int main(){
while (getline(cin,str)){
int pos = str.find('-');
string str1 = str.substr(0, pos);
string str2 = str.substr(pos + 1);
int num1 = getSpaceCount(str1);
int num2 = getSpaceCount(str2);
if (num1 == num2){
//位数相同:个子、对子、三个、四个、顺子
//要么比较牌面大小 要么比较第一个牌的大小
if (str1 == &JOKER& || str2 == &JOKER&) //个子为大王的情况下
cout && &JOKER& &&
int count1 = number(str1.at(0));
int count2 = number(str2.at(0));
(count1&count2) ? cout && str1 && endl :
cout && str2 &&
else if ((num1 == 1 && str1[0] == 'j') || (num2 ==
1&&str2[0] == 'j')){
//有王炸的情况下,一定赢
num1 == 1 ? cout && str1 && endl : cout
&& str2 &&
else if (num1 == 3|| num2 == 3){
//有炸弹情况下(只有一手牌是炸弹),若都是炸弹,则第一步比较完毕,如果有王炸,则第二步已比较完毕
num1 == 3 ? cout && str1 && endl : cout
&& str2 &&
cout && &ERROR& &&
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题
京ICP备号-4
扫一扫,把题目装进口袋

我要回帖

更多关于 java创建一副扑克牌 的文章

 

随机推荐