输出样例
- ( ) + 3 - 3 = 6 | ( ) + 7 - 4 = 7
- ( ) + 5 - 3 = 5 | ( ) - 8 + 8 = 4
- ( ) - 2 + 3 = 4 | 2 + ( ) - 7 = 1
- 3 + ( ) - 6 = 2 | 5 - ( ) + 5 = 4
- 6 + ( ) - 7 = 3 | 8 + ( ) - 9 = 2
- ( ) + 4 - 3 = 7 | 2 - 3 + ( ) = 1
- 8 + 2 - ( ) = 1 | 8 - ( ) + 1 = 1
代码
package com.css.bdpfnew;
import java.util.*;
public class MathAddSubPractice {
public static HashMap<Object, Integer> OPERATOR_COUNT = new HashMap<Object, Integer>() {
{
put(1, 0);
put(2, 0);
put(3, 0);
put(4, 0);
put(5, 0);
put(6, 0);
put(7, 0);
put(8, 0);
put(9, 0);
put("+", 0);
put("-", 0);
}
};
public static ArrayList<String> formulaList = new ArrayList<>();
public static void main(String[] args) {
int i = 0;
while(i < 50000) {
new MathAddSubPractice().questionGenToConfirmResult();
i++;
}
if (formulaList.size() % 2 !=0) {
formulaList.remove(formulaList.size()-1);
}
for (int j = 0; j < formulaList.size(); j = j + 2 ) {
String formula1 = formulaList.get(j);
String formula2 = formulaList.get(j + 1);
System.out.println(String.format("%s | %s", formula1, formula2));
}
for (Map.Entry<Object, Integer> entry : OPERATOR_COUNT.entrySet()) {
String count = String.format("%s : %d", entry.getKey(), entry.getValue());
System.out.println(count);
}
}
public void questionGenToConfirmResult() {
int ran1 = new Random().nextInt(2);
int ran2 = new Random().nextInt(2);
String operator1 = "*";
int operand1 = new Random().nextInt(9) + 1;
String[] operator2 = new String[]{"+", "-"};
int operand2 = new Random().nextInt(9) + 1;
String[] operator3 = new String[]{"+", "-"};
int operand3 = new Random().nextInt(9) + 1;
int result = new Random().nextInt(9) + 1;
List<Object> mathList = new ArrayList<>();
mathList.add(operand1);
mathList.add(operand2);
mathList.add(operand3);
mathList.add(operator1);
mathList.add(operator2[ran1]);
mathList.add(operator3[ran2]);
mathList.add(result);
int operand12 = compute((int)mathList.get(0), (int)mathList.get(1), (String)mathList.get(4));
int operand123 = compute(operand12, (int)mathList.get(2), (String)mathList.get(5));
if (operand123 == result && operand12 > 0) {
for (Object o : mathList) {
if (OPERATOR_COUNT.containsKey(o)) {
OPERATOR_COUNT.put(o, OPERATOR_COUNT.get(o) + 1);
}
}
mathList.set(6, "( )");
String formula = String.format("%s %s %s %s %s = %s", mathList.get(0), mathList.get(4), mathList.get(1), mathList.get(5), mathList.get(2), mathList.get(6));
formulaList.add(formula);
}
}
public void questionGenToConfirmOperand() {
int ran1 = new Random().nextInt(2);
int ran2 = new Random().nextInt(2);
String operator1 = "*";
int operand1 = new Random().nextInt(9) + 1;
String[] operator2 = new String[]{"+", "-"};
int operand2 = new Random().nextInt(9) + 1;
String[] operator3 = new String[]{"+", "-"};
int operand3 = new Random().nextInt(9) + 1;
int result = new Random().nextInt(9) + 1;
List<Object> mathList = new ArrayList<>();
mathList.add(operand1);
mathList.add(operand2);
mathList.add(operand3);
mathList.add(operator1);
mathList.add(operator2[ran1]);
mathList.add(operator3[ran2]);
mathList.add(result);
int operand12 = compute((int)mathList.get(0), (int)mathList.get(1), (String)mathList.get(4));
int operand123 = compute(operand12, (int)mathList.get(2), (String)mathList.get(5));
if (operand123 == result) {
for (Object o : mathList) {
if (OPERATOR_COUNT.containsKey(o)) {
OPERATOR_COUNT.put(o, OPERATOR_COUNT.get(o) + 1);
}
}
mathList.set(3, "+");
int ran3 = new Random().nextInt(3);
mathList.set(ran3, "( )");
List<Integer> leftOpList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
if (i != ran3) {
leftOpList.add(i);
}
}
Integer resultX = 0;
for (Integer op: leftOpList) {
String operator = (String)mathList.get(op + 3);
Integer plusOrMinus = 0;
if (operator.equals("+")) {
plusOrMinus = 1;
} else {
plusOrMinus = -1;
}
Integer operand = (Integer)mathList.get(op);
resultX += operand * plusOrMinus;
}
if (resultX > 0) {
String formula = String.format("%s %s %s %s %s = %s", mathList.get(0), mathList.get(4), mathList.get(1), mathList.get(5), mathList.get(2), mathList.get(6));
formulaList.add(formula);
}
}
}
public int compute(int a, int b, String x) {
if (x == "+") {
return Math.addExact(a, b);
}
return Math.subtractExact(a, b);
}
}