有n张扑克牌,每张牌的取值范围是:2,3,4,5,6,7,8,9,10,J,Q,K,A.在这n张牌中找出顺子(5张及5张以上的连续的牌),并将这些顺子打印出来.
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/14 22:10:31
有n张扑克牌,每张牌的取值范围是:2,3,4,5,6,7,8,9,10,J,Q,K,A.在这n张牌中找出顺子(5张及5张以上的连续的牌),并将这些顺子打印出来.
有n张扑克牌,每张牌的取值范围是:2,3,4,5,6,7,8,9,10,J,Q,K,A.在这n张牌中
找出顺子(5张及5张以上的连续的牌),并将这些顺子打印出来.
有n张扑克牌,每张牌的取值范围是:2,3,4,5,6,7,8,9,10,J,Q,K,A.在这n张牌中找出顺子(5张及5张以上的连续的牌),并将这些顺子打印出来.
import java.util.ArrayList;
import java.util.TreeSet;
public class Main ...{
/** *//**
* @param args
*/
public static void main(String[] args) ...{
String[] cards = ...{"6","2" ,"3" ,"7" ,"4","5","8","8","10","10","10","J","K","Q","A"};
TreeSet set = init(cards); //将字符串表示的扑克牌转换为数字,便于处理
ArrayList result = check(set); //找出顺子
printResult(result); //打印顺子
System.out.println("程序结束!");
}
private static TreeSet init(String[] cards) ...{
TreeSet set = new TreeSet();
for(String s:cards) ...{
if(s.charAt(0)>='1'&&s.charAt(0)4) ...{
result.add(temp.toString());
}
begin = i;
temp.replace(0,temp.length(),numToCard(i)+"");
count = 1;
}
}
if(count>4) result.add(temp.toString());
return result;
}
private static String numToCard(int x) ...{
if(x>10) ...{
switch(x) ...{
case 11:
return "J";
case 12:
return "Q";
case 13:
return "K";
case 14:
return "A";
}
}else ...{
return x+"";
}
return null;
}
//可以将长顺子分解为短顺子的打印模式
private static void printResult1(ArrayList result) ...{
int count = 0;
String[] nodes = null;
if(result.size() == 0) ...{
System.out.println("没有顺子!");
}else ...{
for(String s:result) ...{
nodes = s.split(",");
count += (1+(nodes.length-4))*(nodes.length-4)/2;
}
System.out.println("共有"+count+"个顺子:");
for(String s:result) ...{
nodes = s.split(",");
for(int i=5; i4; j++) ...{
System.out.print(nodes[j].trim());
for(int x = j+1; x