方法一:枚举(穷举)算法
import java.util.*;
public class operator {
*//**
* @param args
*/
/*
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.print("please enter five number:");
int num[] = new int[5];
for(int i=0;i<5;i++){
num[i] = scan.nextInt();
}
System.out.print("the result:");
int result = scan.nextInt();
char[] oper = {'+','-','*','/'};
int count=0; // counter
int i[] = new int[4]; // 循环用 i[0] i[1] i[2] i[3]
for(i[0]=0;i[0]<4;i[0]++){ // (i=0,1,2,3) 0='+', 1='-', 2='*', 3='/'
if(i[0]<3||num[1]!=0){
for(i[1]=0;i[1]<4;i[1]++){
if(i[1]<3||num[2]!=0){
for(i[2]=0;i[2]<4;i[2]++){
if(i[2]<3||num[3]!=0){
for(i[3]=0;i[3]<4;i[3]++){
if(i[3]<3||num[4]!=0){
float left = 0;
float right = num[0];
int sign = 1; // 正负号
for(int j=0;j<4;j++){
switch(oper[i[j]]){
case '+':
left = left+sign*right;
sign = 1;
right = num[j+1];
break;
case '-':
left = left+sign*right;
sign = -1;
right = num[j+1];
break;
case '*':
right = right * num[j+1];
break;
case '/':
right = right / num[j+1];
break;
}
}
if(left+sign*right == result){
count++;
System.out.print(count+":");
for(int j=0;j<4;j++){
System.out.print(num[j]+""+oper[i[j]]+"");
}
System.out.println(num[4]+"="+result);
}
}
}
}
}
}
}
}
}
if(count==0){
System.out.println("没有符合要求的方法!");
}
}
}
方法二;回溯法
import java.util.*;
public class operator {
public static StringBuilder sb; // 用于构成串
public static int num[];
public static int result;
public static int count;
public static void cal(String str)
{
String[] code = str.split("\\d"); // 得到各个运算符
float left = 0;
float right = num[0];
int sign = 1; // 正负号
for (int i = 1; i < code.length; i++) {
switch (code[i].charAt(0)) {
case '+':
left = left + sign * right;
sign = 1;
right = num[i];
break;
case '-':
left = left + sign * right;
sign = -1;
right = num[i];
break;
case '*':
right = right * num[i];
break;
case '/':
right = right / num[i];
break;
}
}
if (left + sign * right == result) {
count++;
System.out.print(count + ":");
for (int j = 0; j < 4; j++) {
System.out.print(num[j] + "" + code[j+1].charAt(0) + "");
}
System.out.println(num[4] + "=" + result);
}
}
public static void combine(int index, int[] num) {
if (index == 5) {
cal(sb.toString());
// System.out.println(sb);
} else {
// 分四种情况,分别为插入“+”,“-”“*”,“/”
for (int i = 0; i < 4; i++) {
// 假设i=0时插入“+”
if (i == 0) {
sb.append("+");
sb.append(num[index]);
combine(index + 1, num);
sb.delete(sb.length() - 2, sb.length());
}
// 假设i=1时插入“-”
if (i == 1) {
sb.append("-");
sb.append(num[index]);
combine(index + 1, num);
sb.delete(sb.length() - 2, sb.length());
}
// 假设i=2时插入“*”
if (i == 2) {
sb.append("*");
sb.append(num[index]);
combine(index + 1, num);
sb.delete(sb.length() - 2, sb.length());
}
// 假设i=1时插入“/”
if (i == 3) {
sb.append("/");
sb.append(num[index]);
combine(index + 1, num);
sb.delete(sb.length() - 2, sb.length());
}
}
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("please enter five number:");
num = new int[5];
for (int i = 0; i < 5; i++) {
num[i] = scan.nextInt();
}
System.out.print("please enter the result:");
result = scan.nextInt();
sb = new StringBuilder();
sb.append(num[0]);
combine(1, num);
if (count == 0) {
System.out.println("没有符合要求的方法!");
}
}
}
运行结果:
please enter five number:5 5 5 5 5
please enter the result:5
1:5+5+5-5-5=5
2:5+5-5+5-5=5
3:5+5-5-5+5=5
4:5+5-5*5/5=5
5:5+5-5/5*5=5
6:5+5*5-5*5=5
7:5+5*5/5-5=5
8:5+5/5-5/5=5
9:5+5/5*5-5=5
10:5-5+5+5-5=5
11:5-5+5-5+5=5
12:5-5+5*5/5=5
13:5-5+5/5*5=5
14:5-5-5+5+5=5
15:5-5*5+5*5=5
16:5-5*5/5+5=5
17:5-5/5+5/5=5
18:5-5/5*5+5=5
19:5*5+5-5*5=5
20:5*5-5*5+5=5
21:5*5*5/5/5=5
22:5*5/5+5-5=5
23:5*5/5-5+5=5
24:5*5/5*5/5=5
25:5*5/5/5*5=5
26:5/5+5-5/5=5
27:5/5-5/5+5=5
28:5/5*5+5-5=5
29:5/5*5-5+5=5
30:5/5*5*5/5=5
31:5/5*5/5*5=5
32:5/5/5*5*5=5
运算符填写问题
最新推荐文章于 2020-03-13 16:58:29 发布