parent
49ac5d1afc
commit
3d016d0eec
@ -1,20 +1,44 @@
|
||||
import java.util.Random;
|
||||
|
||||
public class PrimaryProblem implements ProblemsGenerator {
|
||||
private static final Random RANDOM = new Random();
|
||||
private static final char[] OPS = {'+', '-', '*', '/'};
|
||||
|
||||
@Override
|
||||
public String generate() {
|
||||
int count = RANDOM.nextInt(5) + 1; // 1~5个数
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < count; i++) {
|
||||
int num = RANDOM.nextInt(100) + 1;
|
||||
sb.append(num);
|
||||
if (i < count - 1) {
|
||||
sb.append(" ").append(OPS[RANDOM.nextInt(OPS.length)]).append(" ");
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
import java.util.Random;
|
||||
|
||||
public class PrimaryProblem implements ProblemsGenerator {
|
||||
private static final Random RANDOM = new Random();
|
||||
private static final String[] OPERATORS = {"+", "-", "*", "/"};
|
||||
|
||||
@Override
|
||||
public String generate() {
|
||||
int count = RANDOM.nextInt(4) + 2;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
boolean addParentheses = count >= 3 && RANDOM.nextDouble() < 0.5;
|
||||
if (addParentheses) {
|
||||
int parenCount = RANDOM.nextInt(count - 2) + 2;
|
||||
int startPos = RANDOM.nextInt(count - parenCount + 1);
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (i == startPos) {
|
||||
sb.append("( ");
|
||||
}
|
||||
|
||||
int num = RANDOM.nextInt(100) + 1;
|
||||
sb.append(num);
|
||||
|
||||
if (i == startPos + parenCount - 1) {
|
||||
sb.append(" )");
|
||||
}
|
||||
|
||||
if (i < count - 1) {
|
||||
sb.append(" ").append(OPERATORS[RANDOM.nextInt(OPERATORS.length)]).append(" ");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < count; i++) {
|
||||
int num = RANDOM.nextInt(100) + 1;
|
||||
sb.append(num);
|
||||
if (i < count - 1) {
|
||||
sb.append(" ").append(OPERATORS[RANDOM.nextInt(OPERATORS.length)]).append(" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue