import java.util.Vector;
public class SplendedPoint24 {
public static int[] pokers=new int[4];//四张牌面
public static boolean Result=false,Result2=false;
public static Vector p=new Vector();
public static Vector notations=new Vector();
public static Notation[] nota=new Notation[3];//三个运算符
public static int first=0;
public static int second=0;
public static int third=0;
public static int fourth=0;
public static int z1=0,z2=0,z3=0;
public static Vector strings=new Vector();
private enum Poker{//enum为枚举类
A("A",1),
B("2",2),
C("3",3),
D("4",4),
E("5",5),
F("6",6),
G("7",7),
H("8",8),
I("9",9),
J("10",10),
K("J",11),
L("Q",12),
M("K",13);
private final String Name;
@SuppressWarnings("unused")
private final int Value;
Poker(String name,int value){//Poker构造函数
this.Name=name;
this.Value=value;
}
}
private enum Notation{
Plus_sign("+"),
Subtraction_sign("-"),
Multiplication_sign("×"),
Division_sign("÷"),
Equality_sign("="),
Left_parenthesis("("),
Right_parenthesis(")");
public String Name;
Notation(String name){//Notation构造函数
this.Name=name;
}
}
/**
* <p> Title : getPokers </p>
* <p> Description : 随机产生1~13中间的4张牌 </p>
* @param @return
* @return int[]
* @throws
*/
public static int[] getPokers(){
pokers=new int[4];
for(int i=0;i<pokers.length;i++){
int theValue=(int)(Math.random()*13)+1;
pokers[i]=theValue;
}
return pokers;
}
private static void init(){
p.addElement(Poker.A);//将()里变量加入p尾部
p.addElement(Poker.B);
p.addElement(Poker.C);
p.addElement(Poker.D);
p.addElement(Poker.E);
p.addElement(Poker.F);
p.addElement(Poker.G);
p.addElement(Poker.H);
p.addElement(Poker.I);
p.addElement(Poker.J);
p.addElement(Poker.K);
p.addElement(Poker.L);
p.addElement(Poker.M);
notations.addElement(Notation.Plus_sign);
notations.addElement(Notation.Multiplication_sign);
notations.addElement(Notation.Division_sign);
notations.addElement(Notation.Subtraction_sign);
}
/**
* <p> Title : showPokers </p>
* <p> Description : 显示4张牌的牌面 </p>
* @param
* @return void
* @throws
*/
private static void showPokers(){
init();
System.out.print("得到的牌为:");
for(int poker:pokers){
for(int j=1;j<14;j++){
if(j==poker){
System.out.print(p.get(j-1).Name+"、");//显示牌面
break;
}
}
}
System.out.print("\n");
}
/**
* <p> Title : check </p>
* <p> Description : 枚举法列出构成24点所有可能算法 </p>
* @param @param poker
* @return void
* @throws
*/
public static void check(int[] poker){
pokers=poker;
showPokers();
assert poker.length==4:"Wrong!";
for(int element:poker){
assert (element<14)&&(element>0):"Wrong!";
}//牌数不为四张or牌面超过范围时判定为Wrong
double sum=0;
{
for(int i=0;i<poker.length;i++){
first=poker[i];//第一个数
for(int j=0;(j<poker.length)&&(j!=i);j++){
second=poker[j];//第二个数
for(int m=0;(m<poker.length)&&(m!=j)&&(m!=i);m++){
third=poker[m];//第三个数
for(int n=0;(n<poker.length)&&(n!=m)&&(n!=j)&&(n!=i);n++){
fourth=poker[n];//第四个数
for(;z1<6;z1++){
double s1=sum;
sum=workOut(0,z1,first,second);//前两个数做运算
for(;z2<6;z2++){
double s2=sum;
sum=workOut(1,z2,sum,third);//前三个数做运算
for(;z3<6;z3++){
double s3=sum;
sum=workOut(2,z3,sum,fourth);//四个数做运算
if((sum>=23.999)&&(sum<=24.001)){
// Double类型计算机计算有微小误差
Result=true;
showResult();
Result=Result2=false;
}
sum=s3;
nota[2]=null;
}
z3=0;
sum=s2;
nota[1]=null;
}
z2=0;
sum=s1;
nota[0]=null;
}
z1=0;
}
}
}
}//for1
for(int i=0;i<poker.length;i++){
first=poker[i];
for(int j=0;(j<poker.length)&&(j!=i);j++){
second=poker[j];
for(int m=0;(m<poker.length)&&(m!=j)&&(m!=i);m++){
third=poker[m];
for(int n=0;(n<poker.length)&&(n!=m)&&(n!=j)&&(n!=i);n++){
fourth=poker[n];
for(;z1<6;z1++){
double s1=sum;
sum=workOut(0,z1,first,second);
for(;z2<6;z2++){
double s2=sum;
double sum2=workOut(1,z2,third,fourth);
for(;z3<6;z3++){
double s3=sum;
double s4=sum2;
sum=workOut(2,z3,sum,sum2);
if((sum>=23.999)&&(sum<=24.001)){
Result=true;
Result2=true;
showResult();
Result=Result2=false;
}
sum=s3;
sum2=s4;
nota[2]=null;
}
z3=0;
sum=s2;
nota[1]=null;
}
z2=0;
sum=s1;
nota[0]=null;
}
z1=0;
}
}
}
}//for2
}
if(strings.isEmpty()){
String string="No Result!";
System.out.println(string);
}
strings.removeAllElements();
p.removeAllElements();
notations.removeAllElements();
z1=z2=z3=0;
Result=Result2=false;//归置初始值
}
/**
* <p> Title : showResult </p>
* <p> Description : 组成string表达式 </p>
* @param
* @return void
* @throws
*/
private static void showResult(){
String string="";
if(Result&&!Result2){//为check方法中for1调用
if(z1<4){//牌面为顺序
string=string+Notation.Left_parenthesis.Name+first+nota[0].Name+second+Notation.Right_parenthesis.Name;
}
else{//调整前后牌面的顺序
string=string+Notation.Left_parenthesis.Name+second+nota[0].Name+first+Notation.Right_parenthesis.Name;
}
if(z2<4){
string=Notation.Left_parenthesis.Name+string+nota[1].Name+third+Notation.Right_parenthesis.Name;
}
else{
string=Notation.Left_parenthesis.Name+third+nota[1].Name+string+Notation.Right_parenthesis.Name;
}
if(z3<4){
string=Notation.Left_parenthesis.Name+string+nota[2].Name+fourth+Notation.Right_parenthesis.Name;
}
else{
string=Notation.Left_parenthesis.Name+fourth+nota[2].Name+string+Notation.Right_parenthesis.Name;
}
string=string+Notation.Equality_sign.Name+"24";
strings.addElement(string);
System.out.println(string);
}
else if(Result&&Result2){//为check方法中for2调用
String string2;
if(z1<4){
string=string+Notation.Left_parenthesis.Name+first+nota[0].Name+second+Notation.Right_parenthesis.Name;
}
else{
string=string+Notation.Left_parenthesis.Name+second+nota[0].Name+first+Notation.Right_parenthesis.Name;
}
if(z2<4){
string2=Notation.Left_parenthesis.Name+third+nota[1].Name+fourth+Notation.Right_parenthesis.Name;
}
else{
string2=Notation.Left_parenthesis.Name+fourth+nota[1].Name+third+Notation.Right_parenthesis.Name;
}
if(z3<4){
string=Notation.Left_parenthesis.Name+string+nota[2].Name+string2+Notation.Right_parenthesis.Name;
}
else{
string=Notation.Left_parenthesis.Name+string2+nota[2].Name+string+Notation.Right_parenthesis.Name;
}
string=string+Notation.Equality_sign.Name+"24";
strings.addElement(string);
System.out.println(string);
}
}
/**
* <p> Title : workOut </p>
* <p> Description : 将四个数排列成表达式(初级) </p>
* @param @param not
* @param @param x
* @param @param m
* @param @param n
* @param @return
* @return double
* @throws
*/
private static double workOut(int not,int x,double m,double n){
switch(x){
case 0:
nota[not]=Notation.Plus_sign;
return m+n;
case 1:
nota[not]=Notation.Subtraction_sign;
return m-n;
case 2:
nota[not]=Notation.Multiplication_sign;
return m*n;
case 3:
nota[not]=Notation.Division_sign;
if(n==0){
return Double.POSITIVE_INFINITY;
}
return m/n;
case 4:
nota[not]=Notation.Subtraction_sign;
return n-m;
case 5:
nota[not]=Notation.Division_sign;
if(m==0){
return Double.POSITIVE_INFINITY;
}
return n/m;
default:
System.out.println("Wrong!");
return 0;
}
}
public static void main(String[] args){
int[] p1={3,2,8,2};//自定义输入情况
check(p1);
int[] poker={12,9,3,4};//输入其中牌面不为JQK情况
check(poker);
int[] poker1=getPokers();//随机产生四个数
check(poker1);
}
}
一.算法设计思路
该部分已在程序注释中体现
二.相关部分知识点
Vector:
为一个动态数组,有以下四种构造方法:
Vector()
Vector(int size)
Vector(int size,int incr)
Vector(Collection c)
主要方法:
void addElement(Object obj)
将指定的组件添加到此向量的末尾,将其大小增加 1
enum:
枚举
可以是常量、对象、带字段和方法、singleleton