任意1-10中的4个数字,使用加减乘除计算得出24结果的可能组合(java版),很多人小时候都玩过...
import .;
import .*;
/**
* Author:
* Date:
*/
public class Point24Caculator {
/**
* 计算24点中可以到的操作
*/
private static String[] operators = {
"+", "-", "*", "/"
};
public static void main(String[] args) throws Exception {
double[] array = new double[4];
int index=0;
Scanner scanner=new Scanner();
while (index<4)
{
(("请输入第%s个1-10的整数",index+1));
String tempNumStr=();
if(!(tempNumStr))
{
("你输入的不是一个整数");
continue;
}
double tmpNum=(tempNumStr);
if (tmpNum<0 || tmpNum>10)
{
("你输入的数字不是1-10的数字");
continue;
}
array[index++]=tmpNum;
}
(("你输入的4个1-10的整数为%s,%s,%s,%s",array[0],array[1],array[2],array[3]));
("结果如下:");
List<double[]> resultAllList = new ArrayList<>();
List<double[]> list = new ArrayList<>();
(array);
(new double[]{array[1], array[2], array[3], array[0]});
(new double[]{array[2], array[3], array[0], array[1]});
(new double[]{array[3], array[0], array[1], array[2]});
for (int i = 0; i < (); i++) {
getAllArray(resultAllList, ((i), (i).length));
}
int sum = 0;
Iterator<double[]> iterator = ();
while (()) {
double[] tempArray = ();
sum += caculate24Point(tempArray);
sum += caculate24Point2(tempArray);
}
("总共方案数量:" + sum);
}
/**
* 获取array的所有可能组合
*
* @param list
* @param array
*/
public static void getAllArray(List<double[]> list, double[] array) {
if (!exists(list, array)) {
(array);
}
for (int i = 1; i < 4; i++) {
double[] arrayCopy = (array, );
List<double[]> newList = getArrayList(arrayCopy, i);
Iterator<double[]> iterator = ();
while (()) {
double[] temp = ();
if (!exists(list, temp)) {
(temp);
}
}
}
}
/**
* 获取array下标遇到i的位置左右组合
*
* @param array
* @param i
* @return
*/
public static List<double[]> getArrayList(double[] array, int i) {
List<double[]> list = new ArrayList<>();
for (int j = i; j > 0; j--) {
double temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
(array);
array = (array, );
}
return list;
}
/**
* array是否存啊在list中
*
* @param list
* @param array
* @return
*/
public static boolean exists(List<double[]> list, double[] array) {
Iterator<double[]> iterator = ();
while (()) {
double[] tmpArray = ();
if (tmpArray[0] == array[0] && tmpArray[1] == array[1] && tmpArray[2] == array[2] && tmpArray[3] == array[3]) {
return true;
}
}
return false;
}
/**
* 计算array能算24点的所有组合,从左到右的顺序
*
* @param
* @throws Exception
*/
public static int caculate24Point(double[] array) throws Exception {
int count = 0;
if ( != 4) {
throw new Exception("不是四个数");
}
for (String op : operators) {
String expressionStr = "";
double result = getTwoNumCaculate(array[0], array[1], op);
if (!isValidResult(result))
{
continue;
}
expressionStr = ("(%s %s %s)", array[0], op, array[1]);
for (String op2 : operators) {
double result1 = getTwoNumCaculate(result, array[2], op2);
if (!isValidResult(result1))
{
continue;
}
String expressionStr2 = ("(%s %s %s)", expressionStr, op2, array[2]);
for (String op3 : operators) {
double result2 = getTwoNumCaculate(result1, array[3], op3);
String expressionStr3 = ("%s %s %s", expressionStr2, op3, array[3]);
if (result2 == 24.0d) {
count++;
(("方案:%s=%s", expressionStr3, result2));
}
}
}
}
return count;
}
/**
* 计算array能算24点的所有组合 ,两两组合
*
* @param array
* @return
* @throws Exception
*/
public static int caculate24Point2(double[] array) throws Exception {
int count = 0;
if ( != 4) {
throw new Exception("不是四个数");
}
for (String op : operators) {
double result1 = getTwoNumCaculate(array[0], array[1], op);
if (!isValidResult(result1))
{
continue;
}
String expressionStr1 = ("(%s %s %s)", array[0], op, array[1]);
for (String op2 : operators) {
double result2 = getTwoNumCaculate(array[2], array[3], op2);
if (!isValidResult(result2))
{
continue;
}
String expressionStr2 = ("(%s %s %s)", array[2], op2, array[3]);
for (String op3 : operators) {
double result3 = getTwoNumCaculate(result1, result2, op3);
String expressionStr3 = ("%s %s %s", expressionStr1, op3, expressionStr2);
if (result3 == 24.0d) {
count++;
(("方案: %s = %s", expressionStr3, result3));
}
}
}
}
return count;
}
/**
* 是否为合法的计算结果
* @param result
* @return
*/
public static boolean isValidResult(double result){
if (result<1)
{
return false;
}
return result == (result);
}
private static double getTwoNumCaculate(double num1, double num2, String operator) throws Exception {
switch (operator) {
case "+":
return num1 + num2;
case "-":
return num1 - num2;
case "*":
return num1 * num2;
case "/":
return num1 / num2;
default:
throw new Exception("运算符不符合规范");
}
}
}