撬动offer:寻找丢失的数字
共 4570字,需浏览 10分钟
·
2020-11-29 00:24
点击上方「蓝字」关注我们
0x01:算法题目
Missing Digit
Have the function MissingDigit(str) take the str parameter, which will be a simple mathematical formula with three numbers, a single operator (+, -, *, or /) and an equal sign (=) and return the digit that completes the equation. In one of the numbers in the equation, there will be an x character, and your program should determine what digit is missing. For example, if str is "3x + 12 = 46" then your program should output 4. The x character can appear in any of the three numbers and all three numbers will be greater than or equal to 0 and less than or equal to 1000000.
Examples
Input: "4 - 2 = x"
Output: 2
Input: "1x0 * 12 = 1200"
Output: 0
大概的意思:存在一个四则运算等式,包含三个数字、一个符号(可以是+、-、*、/)和一个等号(=)。其中三字数字是可以是等式成立的,在三个数字中有个数字中有一个数字的某一位是未知的,求这位的数字。
0x02:题解
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class MainCmd {
public static String MissingDigit(String str) {
if (str == null || str.trim().equals("")) {
return str;
}
if (!str.contains("=")) {
return str;
}
String[] splitStr = str.split("=");
String firstElement = splitStr[0];
if (firstElement.contains("-") || firstElement.contains("+") || firstElement.contains("*")
|| firstElement.contains("/")) {
String[] ele = firstElement.split("[\\+\\-\\*\\/]");
String eleOne = ele[0];
String eleTow = ele[1];
String eleThree = splitStr[1];
String result = "";
if (str.contains("+")) {
result = process(eleOne, eleTow, eleThree, "+");
} else if (str.contains("-")) {
result = process(eleOne, eleTow, eleThree, "-");
} else if (str.contains("*")) {
result = process(eleOne, eleTow, eleThree, "*");
} else {
result = process(eleOne, eleTow, eleThree, "/");
}
return result;
} else {
return str;
}
}
public static boolean checkContainX(String str) {
if (str.contains("x")) {
return true;
}
return false;
}
private static String process(String eleOne, String eleTow, String eleThree, String syboml) {
boolean eleOneFlag = checkContainX(eleOne);
boolean eleTowFlag = checkContainX(eleTow);
boolean eleThreeFlag = checkContainX(eleThree);
long result = 0;
switch (syboml) {
case "+":
if(eleOneFlag){
result = Long.parseLong(eleThree) - Long.parseLong(eleTow);
}else if(eleTowFlag){
result = Long.parseLong(eleThree) - Long.parseLong(eleOne);
}else if(eleThreeFlag){
result = Long.parseLong(eleOne) + Long.parseLong(eleTow);
}
break;
case "-":
if(eleOneFlag){
result = Long.parseLong(eleThree) + Long.parseLong(eleTow);
}else if(eleTowFlag){
result = Long.parseLong(eleOne) - Long.parseLong(eleThree);
}else if(eleThreeFlag){
result = Long.parseLong(eleOne) - Long.parseLong(eleTow);
}
break;
case "*":
if(eleOneFlag){
result = Long.parseLong(eleThree) / Long.parseLong(eleTow);
}else if(eleTowFlag){
result = Long.parseLong(eleThree) / Long.parseLong(eleOne);
}else if(eleThreeFlag){
result = Long.parseLong(eleOne) * Long.parseLong(eleTow);
}
break;
case "/":
if(eleOneFlag){
result = Long.parseLong(eleThree) * Long.parseLong(eleTow);
}else if(eleTowFlag){
result = Long.parseLong(eleOne) / Long.parseLong(eleThree);
}else if(eleThreeFlag){
result = Long.parseLong(eleOne) / Long.parseLong(eleTow);
}
break;
}
String retResult = "";
if(eleOneFlag){
retResult = getX(result, eleOne);
}else if(eleTowFlag){
retResult = getX(result, eleTow);
}else if(eleThreeFlag){
retResult = getX(result, eleThree);
}
return retResult;
}
private static String getX(Long result, String ele) {
String target = String.valueOf(result);
char[] targetChar = target.toCharArray();
char[] eleChar = ele.toCharArray();
Map< Character, Character> mapper = new HashMap();
for(int i=0; i mapper.put(eleChar[i], targetChar[i]);
}
return String.valueOf(mapper.get('x'));
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(true){
String line = s.nextLine();
line = line.replaceAll(" ", "");
System.out.print(MissingDigit(line));
}
}
}
这个是暴力破解出来的,集思广益,看看大家有没有比较优的方案。
扫码二维码
获取更多精彩
Java乐园