输入测试:
2//测试数量
X7//第一个加数 9X//第二个加数 123//结果(其中X表示需要猜测的数字)
X X 4
输出:
1
3
最简单的穷举(没超时间)
package test1;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
//判断res-op1是否满足s的格式
static boolean valid(int op1, int res, String s) {
int op2 = res - op1;
if (op2 < 0) {
return false;
}
String sop2 = "" + op2;
for (int i = sop2.length(); i < 3; ++i) {
sop2 = "0" + sop2;
}
if (sop2.length() != s.length()) {
return false;
}
for (int i = 0; i < s.length(); ++i) {
if (!(s.charAt(i) == sop2.charAt(i) || s.charAt(i) == 'X')) {
return false;
}
}
return true;
}
//判断首位X是否不为0
static boolean valid2(int op1, int op2, String s1, String s2) {
int index1 = s1.indexOf("X");
int index2 = s2.indexOf("X");
String sop1 = "" + op1;
for (int i = sop1.length(); i < 3; ++i) {
sop1 = "0" + sop1;
}
String sop2 = "" + op2;
for (int i = sop2.length(); i < 3; ++i) {
sop2 = "0" + sop2;
}
for (int i = 0; i < s1.length(); ++i) {
if(s1.charAt(i) != '0' && s1.charAt(i) != 'X'){
if(i < index1){
index1 = i;
break;
}
}
}
for (int i = 0; i < s2.length(); ++i) {
if(s2.charAt(i) != '0' && s2.charAt(i) != 'X'){
if(i < index2){
index2 = i;
break;
}
}
}
if (s1.charAt(index1) == 'X' && sop1.charAt(index1) == '0') {
return false;
}
if (s2.charAt(index2) == 'X' && sop2.charAt(index2) == '0') {
return false;
}
return true;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int t = 0; t < T; ++t) {
String s1 = in.next();
String s2 = in.next();
String s3 = in.next();
int res = Integer.parseInt(s3);
for (int i = s1.length(); i < 3; ++i) {
s1 = "0" + s1;
}
for (int i = s2.length(); i < 3; ++i) {
s2 = "0" + s2;
}
String op1 = new String(s1);
String op2 = new String(s2);
op1 = op1.replaceAll("X", "0");
op2 = op2.replaceAll("X", "0");
int count = 0;
int end0 = 10;
int end1 = 10;
int end2 = 10;
for (int i = 0; i < end0; ++i) {
end1 = 10;
StringBuilder op11 = new StringBuilder(op1);
boolean m = false;
if (s1.charAt(0) == 'X') {
op11.setCharAt(0, (char) ('0' + i));
m = true;
} else {
end0 = 1;
}
for (int ii = 0; ii < end1; ++ii) {
end2 = 10;
if (s1.charAt(1) == 'X') {
op11.setCharAt(1, (char) ('0' + ii));
m = true;
} else {
end1 = 1;
}
for (int iii = 0; iii < end2; ++iii) {
if (s1.charAt(2) == 'X') {
op11.setCharAt(2, (char) ('0' + iii));
m = true;
} else {
end2 = 1;
}
if (m) {
m = false;
int op111 = Integer.parseInt(op11.toString());
if (valid(op111, res, s2)) {
int op222 = res - op111;
if (valid2(op111, op222, s1, s2)) {
++count;
}
}
}
}
}
}
System.out.println(count);
}
}
}