早教吧作业答案频道 -->其他-->
随机输入四个数字,用加减乘除得到期望值,用java语言比如说我输入1234,我的期望值是10,那么有1+2+3+4=10,还有3*4-1*2=10就是要把输入的数字的各个位通过加减乘除得到输入的期望值的这个数字.
题目详情
随机输入四个数字,用加减乘除得到期望值,用java语言
比如说 我输入1234,我的期望值是10,那么有1+2+3+4=10,还有3*4-1*2=10 就是要把输入的数字的各个位通过加减乘除得到输入的期望值的这个数字.
希望有人帮忙,很着急,最好今天下午能写好~希望有些注释,我的java水平不是十分高,
如果有人写的好可以追加悬赏~
比如说 我输入1234,我的期望值是10,那么有1+2+3+4=10,还有3*4-1*2=10 就是要把输入的数字的各个位通过加减乘除得到输入的期望值的这个数字.
希望有人帮忙,很着急,最好今天下午能写好~希望有些注释,我的java水平不是十分高,
如果有人写的好可以追加悬赏~
▼优质解答
答案和解析
package test;
import java.util.Arrays;
import java.util.Scanner;
public class Compare {
Scanner in = new Scanner(System.in);
public static int z = 0;
public static int expectedValue;
public static int a;
public static int b;
public static int c;
public static int d;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入四个数字...");
a = in.nextInt();
b = in.nextInt();
c = in.nextInt();
d = in.nextInt();
System.out.println("请输入期望值...");
expectedValue = in.nextInt();
System.out.println("四个数字是:"+a+","+b+","+c+","+d+"..期望值是:expectedValue="+expectedValue);
int[] array = { a, b, c, d };
Arrays.sort(array);
StringBuffer buffer = new StringBuffer();
for (int o = 0; o < array.length; o++) {
a = array[o];
for (int p = 0; p < array.length; p++) {
if (p != o) {
b = array[p];
for (int q = 0; q < array.length; q++) {
if (q != o && q != p) {
c = array[q];
for (int index = 0; index < array.length; index++) {
if (index != o && index != p && index != q) {
d = array[index];
for (double i = 0; i < 2; i++) {
if (i == 0) {
for (double j = 0; j < array.length; j++) {
for (double k = 0; k < array.length; k++) {
fun(oper(a, b, j), arith(c, d, k), j, k,buffer);
}
}
}
if (i == 1) {
for (int j = 0; j < array.length; j++) {
for (int k = 0; k < array.length; k++) {
double sum = arith(oper(a,
b, j), c, k);
func(sum, d, j, k,buffer);
}
}
}
}
}
}
}
}
}
}
}
}
private static void func(double i, double j, int j2, int k2,StringBuffer buffer) {
String str = "";
String str1 = "";
if (j2 == 0) {str = "+";
} else if (j2 == 1) {str = "-";
} else if (j2 == 2) {str = "*";
} else {str = "/";}
if (k2 == 0) {str1 = "+";
} else if (k2 == 1) {str1 = "-";
} else if (k2 == 2) {str1 = "*";
} else {str1 = "/";}
for (double k = 0; k < 4; k++) {
if (k == 0) {
if ((i + j) == expectedValue) {
String bb = "[(" + a + str + b + ")" + str1 + c
+ "]" + "+" + d + "=" + expectedValue;
if(buffer.indexOf(bb) == -1){
System.out.println(bb);
buffer.append(bb);
}
}
}
if (k == 1) {
if ((i - j) == expectedValue) {
String bb = "[(" + a + str + b + ")" + str1 + c
+ "]" + "-" + d + "=" + expectedValue;
if(buffer.indexOf(bb) == -1){
System.out.println(bb);
buffer.append(bb);
}
}
}
if (k == 2) {
if ((i * j) == expectedValue) {
String bb = "[(" + a + str + b + ")" + str1 + c
+ "]" + "*" + d + "=" + expectedValue;
if(buffer.indexOf(bb) == -1){
System.out.println(bb);
buffer.append(bb);
}
}
}
if (k == 3) {
if ((i / j) == expectedValue) {
String bb = "[(" + a + str + b + ")" + str1 + c
+ "]" + "/" + d + "=" + expectedValue;
if(buffer.indexOf(bb) == -1){
System.out.println(bb);
buffer.append(bb);
}
}
}
}
}
private static void fun(double i, double j, double j2, double k2,StringBuffer buffer) {
String str = "";
String str1 = "";
if (j2 == 0) {str = "+";
} else if (j2 == 1) {str = "-";
} else if (j2 == 2) {str = "*";
} else {str = "/"; }
if (k2 == 0) {str1 = "+";
} else if (k2 == 1) {str1 = "-";
} else if (k2 == 2) {str1 = "*";
} else {str1 = "/"; }
for (double k = 0; k < 4; k++) {
if (k == 0) {
if ((i + j) == expectedValue) {
String bb = "(" + a + str + b + ")+(" + c + str1 + d + ")="
+ expectedValue;
if (buffer.indexOf(bb) == -1) {
System.out.println(bb);
buffer.append(bb);
}
}
}
if (k == 1) {
if ((i - j) == expectedValue) {
String bb = "(" + a + str + b + ")-(" + c + str1 + d + ")="
+ expectedValue;
if (buffer.indexOf(bb) == -1) {
System.out.println(bb);
buffer.append(bb);
}
}
}
if (k == 2) {
if ((i * j) == expectedValue) {
String bb = "(" + a + str + b + ")*(" + c + str1 + d + ")="
+ expectedValue;
if (buffer.indexOf(bb) == -1) {
System.out.println(bb);
buffer.append(bb);
}
}
}
if (k == 3) {
if ((i / j) == expectedValue) {
String bb = "(" + a + str + b + ")/(" + c + str1 + d + ")="
+ expectedValue;
if (buffer.indexOf(bb) == -1) {
System.out.println(bb);
buffer.append(bb);
}
}
}
}
}
public static double oper(double a, double b, double j) {
double z = 0;
if (j == 0) {
z = a + b;
} else if (j == 1) {
z = a - b;
} else if (j == 2) {
z = a * b;
} else if (j == 3) {
z = a / b;
}
return z;
}
public static double arith(double c, double d, double k) {
double z = 0;
if (k == 0) {
z = c + d;
} else if (k == 1) {
z = c - d;
} else if (k == 2) {
z = c * d;
} else if (k == 3) {
z = c / d;
}
return z;
}
}
测试数据:
请输入四个数字...
1
1
9
9
请输入期望值...
10
四个数字是:1,1,9,9..期望值是:expectedValue=10
[(1/9)+1]*9=10
import java.util.Arrays;
import java.util.Scanner;
public class Compare {
Scanner in = new Scanner(System.in);
public static int z = 0;
public static int expectedValue;
public static int a;
public static int b;
public static int c;
public static int d;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入四个数字...");
a = in.nextInt();
b = in.nextInt();
c = in.nextInt();
d = in.nextInt();
System.out.println("请输入期望值...");
expectedValue = in.nextInt();
System.out.println("四个数字是:"+a+","+b+","+c+","+d+"..期望值是:expectedValue="+expectedValue);
int[] array = { a, b, c, d };
Arrays.sort(array);
StringBuffer buffer = new StringBuffer();
for (int o = 0; o < array.length; o++) {
a = array[o];
for (int p = 0; p < array.length; p++) {
if (p != o) {
b = array[p];
for (int q = 0; q < array.length; q++) {
if (q != o && q != p) {
c = array[q];
for (int index = 0; index < array.length; index++) {
if (index != o && index != p && index != q) {
d = array[index];
for (double i = 0; i < 2; i++) {
if (i == 0) {
for (double j = 0; j < array.length; j++) {
for (double k = 0; k < array.length; k++) {
fun(oper(a, b, j), arith(c, d, k), j, k,buffer);
}
}
}
if (i == 1) {
for (int j = 0; j < array.length; j++) {
for (int k = 0; k < array.length; k++) {
double sum = arith(oper(a,
b, j), c, k);
func(sum, d, j, k,buffer);
}
}
}
}
}
}
}
}
}
}
}
}
private static void func(double i, double j, int j2, int k2,StringBuffer buffer) {
String str = "";
String str1 = "";
if (j2 == 0) {str = "+";
} else if (j2 == 1) {str = "-";
} else if (j2 == 2) {str = "*";
} else {str = "/";}
if (k2 == 0) {str1 = "+";
} else if (k2 == 1) {str1 = "-";
} else if (k2 == 2) {str1 = "*";
} else {str1 = "/";}
for (double k = 0; k < 4; k++) {
if (k == 0) {
if ((i + j) == expectedValue) {
String bb = "[(" + a + str + b + ")" + str1 + c
+ "]" + "+" + d + "=" + expectedValue;
if(buffer.indexOf(bb) == -1){
System.out.println(bb);
buffer.append(bb);
}
}
}
if (k == 1) {
if ((i - j) == expectedValue) {
String bb = "[(" + a + str + b + ")" + str1 + c
+ "]" + "-" + d + "=" + expectedValue;
if(buffer.indexOf(bb) == -1){
System.out.println(bb);
buffer.append(bb);
}
}
}
if (k == 2) {
if ((i * j) == expectedValue) {
String bb = "[(" + a + str + b + ")" + str1 + c
+ "]" + "*" + d + "=" + expectedValue;
if(buffer.indexOf(bb) == -1){
System.out.println(bb);
buffer.append(bb);
}
}
}
if (k == 3) {
if ((i / j) == expectedValue) {
String bb = "[(" + a + str + b + ")" + str1 + c
+ "]" + "/" + d + "=" + expectedValue;
if(buffer.indexOf(bb) == -1){
System.out.println(bb);
buffer.append(bb);
}
}
}
}
}
private static void fun(double i, double j, double j2, double k2,StringBuffer buffer) {
String str = "";
String str1 = "";
if (j2 == 0) {str = "+";
} else if (j2 == 1) {str = "-";
} else if (j2 == 2) {str = "*";
} else {str = "/"; }
if (k2 == 0) {str1 = "+";
} else if (k2 == 1) {str1 = "-";
} else if (k2 == 2) {str1 = "*";
} else {str1 = "/"; }
for (double k = 0; k < 4; k++) {
if (k == 0) {
if ((i + j) == expectedValue) {
String bb = "(" + a + str + b + ")+(" + c + str1 + d + ")="
+ expectedValue;
if (buffer.indexOf(bb) == -1) {
System.out.println(bb);
buffer.append(bb);
}
}
}
if (k == 1) {
if ((i - j) == expectedValue) {
String bb = "(" + a + str + b + ")-(" + c + str1 + d + ")="
+ expectedValue;
if (buffer.indexOf(bb) == -1) {
System.out.println(bb);
buffer.append(bb);
}
}
}
if (k == 2) {
if ((i * j) == expectedValue) {
String bb = "(" + a + str + b + ")*(" + c + str1 + d + ")="
+ expectedValue;
if (buffer.indexOf(bb) == -1) {
System.out.println(bb);
buffer.append(bb);
}
}
}
if (k == 3) {
if ((i / j) == expectedValue) {
String bb = "(" + a + str + b + ")/(" + c + str1 + d + ")="
+ expectedValue;
if (buffer.indexOf(bb) == -1) {
System.out.println(bb);
buffer.append(bb);
}
}
}
}
}
public static double oper(double a, double b, double j) {
double z = 0;
if (j == 0) {
z = a + b;
} else if (j == 1) {
z = a - b;
} else if (j == 2) {
z = a * b;
} else if (j == 3) {
z = a / b;
}
return z;
}
public static double arith(double c, double d, double k) {
double z = 0;
if (k == 0) {
z = c + d;
} else if (k == 1) {
z = c - d;
} else if (k == 2) {
z = c * d;
} else if (k == 3) {
z = c / d;
}
return z;
}
}
测试数据:
请输入四个数字...
1
1
9
9
请输入期望值...
10
四个数字是:1,1,9,9..期望值是:expectedValue=10
[(1/9)+1]*9=10
看了 随机输入四个数字,用加减乘除...的网友还看了以下:
次数除了字母的指数以外,还包括数字的字母吗?(表示是初一学生)错了是还包括数字的指数吗? 2020-05-22 …
小明:这篇文章,我已经打了总字数的40%,还剩下360个字小红:你再打这篇文章字数的35%,还剩下 2020-06-03 …
C语言问题求助第4章分支程序设计1.输入若干整数给x,使用if语句输出x的绝对值,当输入0时结束。 2020-06-09 …
请问,左边一个羽毛的“羽”字,右边一个工人的“工”字,这个字读音是什么,意义是什么,怎样输入?字典 2020-06-18 …
excel2007如何在第一张表输入数字,第二张表自动跳到对应的数字上?比如表1车号天数13211 2020-07-07 …
2、编写函数求字符串的子串,在主函数中输入子串作为参数,字符串长度不超过255.例如:由键盘依次输 2020-07-19 …
关于海棠花的好句还能输入499字还能输入491字 2020-07-21 …
c程序问题,只执行第一步a+b,而且计算出来的a+b是错误的.为啥会这样,哪里错了?:输入两个整数a 2020-11-23 …
pb中如何判断在sle1中录入的是数字还是字母?因为在一个find语句中,有一个字段(bigint型 2020-12-22 …
求数字排列将1-19这19个数字分别填入正六边形的六个顶点,六边的中点,还有中心点以及中心点和顶点连 2020-12-24 …