Java 算24點
阿新 • • 發佈:2019-02-12
import java.util.Scanner;
//測試過了,大概的都能算出來,不過bug還是有點多,發現的評論區說一下我再改改
public class The_24 {
public static void main(String[] args) {
int[] a = new int[4];// 四個數字
for (int i = 0; i < 4; i++) {
a[i] = shuZhi();
}
shuZi(a);
}
// 輸入數值
public static int shuZhi() {//實現判斷輸入數值是否符合24點遊戲規則
Scanner s = new Scanner(System.in);
int a = s.nextInt();
while (a < 1 || a > 13) {
System.out.println("輸入的數值不對,請重新輸入");
a = s.nextInt();
}
return a;
}
// 數字的組合
public static void shuZi(int[] a) {//a.b,c,d的排列組合
char[] f = { '+', '-', '*', '/' };// 運算子
int flag = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
for (int l = 0; l < 4; l++) {
if (i != j && i != k && i != l && j != k && j != l && k != l) {// a,b,c,d不重複
if (zuHe(f, a[i], a[j], a[k], a[l]) == 1) {
flag = 1;
}
}
}
}
}
}
if (flag == 0) {
System.out.println("無解");
}
}
// 式子的組合
public static int zuHe(char[] f, int a1, int a2, int a3, int a4) {
String s = "" + a1;
int flag = 0;
for (int i = 0; i < 4; i++) {// 運算子的與數字的排列組合(這裡可能不全面,有更好方法的評論區說一下,謝謝)
String s1 = s + f[i] + a2;
for (int j = 0; j < 4; j++) {
String s2 = s1 + f[j] + a3;
for (int k = 0; k < 4; k++) {
String s3 = s2 + f[k] + a4;
if (biaoDaShi(s3) == 1) {
flag = 1;
}
s3 = s2;
}
}
}
return flag;
}
// 運算子的優先順序
public static int yunSuan(char c) {
if (c == '+' || c == '-') {
return 1;
} else if (c == '*' || c == '/') {
return 2;
} else
return 0;
}
// 表示式求值
public static int biaoDaShi(String s1) {
String s = s1 + "#";
String n = "";
double[] zhan1 = new double[100];// 數值棧
char[] zhan2 = new char[100];// 運算子棧
zhan2[0] = '#';
char a;// 當前元素
int flag = 0, x = 1, top1 = -1, top2 = 0;// 棧指標
for (int i = 0; i < s.length(); i++)// 轉換為字尾表示式
{
a = s.charAt(i);
if (a <= 57 && a >= 48)// 判斷是否為數字
{
n = n + a;
x++;
} else
x = 0;
if (x == 0 && n != "") {
top1++;
zhan1[top1] = Integer.valueOf(n);
n = "";
}
if (a == '+' || a == '-' || a == '*' || a == '/' || a == '(' || a == ')' || a == '#') {
for (int j = 0; j <= top2; j++) {
if (yunSuan(a) <= yunSuan(zhan2[top2]) && a != '(') {
switch (zhan2[top2]) {
case '+':
zhan1[top1 - 1] = zhan1[top1 - 1] + zhan1[top1];
top1--;
top2--;
break;
case '-':
zhan1[top1 - 1] = zhan1[top1 - 1] - zhan1[top1];
top1--;
top2--;
break;
case '*':
zhan1[top1 - 1] = zhan1[top1 - 1] * zhan1[top1];
top1--;
top2--;
break;
case '/':
zhan1[top1 - 1] = zhan1[top1 - 1] / zhan1[top1];
top1--;
top2--;
break;
case '(':
top2--;
break;
case '#':
top2--;
break;
default:
top2--;
break;
}
}
}
if (yunSuan(a) > yunSuan(zhan2[top2]) || a == '(') {
top2++;
zhan2[top2] = a;
}
}
}
if (top1 == 0 && top2 == 0) {// 運算結束
if (zhan1[0] == 24) {//結果等於24就輸出
System.out.println(s1 + "=" + zhan1[0]);
flag = 1;
}
} else
System.out.print("no");
return flag;
}
//測試過了,大概的都能算出來,不過bug還是有點多,發現的評論區說一下我再改改
public class The_24 {
public static void main(String[] args) {
int[] a = new int[4];// 四個數字
for (int i = 0; i < 4; i++) {
a[i] = shuZhi();
}
shuZi(a);
}
// 輸入數值
public static int shuZhi() {//實現判斷輸入數值是否符合24點遊戲規則
Scanner s = new Scanner(System.in);
int a = s.nextInt();
while (a < 1 || a > 13) {
System.out.println("輸入的數值不對,請重新輸入");
a = s.nextInt();
}
return a;
}
// 數字的組合
public static void shuZi(int[] a) {//a.b,c,d的排列組合
char[] f = { '+', '-', '*', '/' };// 運算子
int flag = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
for (int l = 0; l < 4; l++) {
if (i != j && i != k && i != l && j != k && j != l && k != l) {// a,b,c,d不重複
if (zuHe(f, a[i], a[j], a[k], a[l]) == 1) {
flag = 1;
}
}
}
}
}
}
if (flag == 0) {
System.out.println("無解");
}
}
// 式子的組合
public static int zuHe(char[] f, int a1, int a2, int a3, int a4) {
String s = "" + a1;
int flag = 0;
for (int i = 0; i < 4; i++) {// 運算子的與數字的排列組合(這裡可能不全面,有更好方法的評論區說一下,謝謝)
String s1 = s + f[i] + a2;
for (int j = 0; j < 4; j++) {
String s2 = s1 + f[j] + a3;
for (int k = 0; k < 4; k++) {
String s3 = s2 + f[k] + a4;
if (biaoDaShi(s3) == 1) {
flag = 1;
}
s3 = s2;
}
}
}
return flag;
}
// 運算子的優先順序
public static int yunSuan(char c) {
if (c == '+' || c == '-') {
return 1;
} else if (c == '*' || c == '/') {
return 2;
} else
return 0;
}
// 表示式求值
public static int biaoDaShi(String s1) {
String s = s1 + "#";
String n = "";
double[] zhan1 = new double[100];// 數值棧
char[] zhan2 = new char[100];// 運算子棧
zhan2[0] = '#';
char a;// 當前元素
int flag = 0, x = 1, top1 = -1, top2 = 0;// 棧指標
for (int i = 0; i < s.length(); i++)// 轉換為字尾表示式
{
a = s.charAt(i);
if (a <= 57 && a >= 48)// 判斷是否為數字
{
n = n + a;
x++;
} else
x = 0;
if (x == 0 && n != "") {
top1++;
zhan1[top1] = Integer.valueOf(n);
n = "";
}
if (a == '+' || a == '-' || a == '*' || a == '/' || a == '(' || a == ')' || a == '#') {
for (int j = 0; j <= top2; j++) {
if (yunSuan(a) <= yunSuan(zhan2[top2]) && a != '(') {
switch (zhan2[top2]) {
case '+':
zhan1[top1 - 1] = zhan1[top1 - 1] + zhan1[top1];
top1--;
top2--;
break;
case '-':
zhan1[top1 - 1] = zhan1[top1 - 1] - zhan1[top1];
top1--;
top2--;
break;
case '*':
zhan1[top1 - 1] = zhan1[top1 - 1] * zhan1[top1];
top1--;
top2--;
break;
case '/':
zhan1[top1 - 1] = zhan1[top1 - 1] / zhan1[top1];
top1--;
top2--;
break;
case '(':
top2--;
break;
case '#':
top2--;
break;
default:
top2--;
break;
}
}
}
if (yunSuan(a) > yunSuan(zhan2[top2]) || a == '(') {
top2++;
zhan2[top2] = a;
}
}
}
if (top1 == 0 && top2 == 0) {// 運算結束
if (zhan1[0] == 24) {//結果等於24就輸出
System.out.println(s1 + "=" + zhan1[0]);
flag = 1;
}
} else
System.out.print("no");
return flag;
}
}
//<Date 2018-6.16>