Untitled
unknown
plain_text
3 years ago
2.8 kB
5
Indexable
package com.company; import java.util.Scanner; public class Main { static boolean not_found; static boolean check_expression(String expression, char math_symbol) { String operand1 = expression.substring(0, 2); String operand2 = expression.substring(5, 7); String ans = expression.substring(10); int op1 = Integer.parseInt(operand1); int op2 = Integer.parseInt(operand2); int kq = Integer.parseInt(ans); if (math_symbol == '+') { return (op1 + op2 == kq); } if (math_symbol == '-') { return (op1 - op2 == kq); } if (math_symbol == '/') { return (op1 / op2 == kq); } return (op1 * op2 == kq); } static void Try(String expression, char math_symbol, int k) { if (not_found) { for (int i = k; i < 12; i++) { if (expression.charAt(i) == '?') { if (i == 0 || expression.charAt(i - 1) == ' ') { for (int j = 1; j <= 9; j++) { System.out.println('0' + j); Try(expression.substring(0, i) + ('0' + j) + expression.substring(i + 1), math_symbol, i + 1); } } else { for (int j = 1; j <= 9; j++) { System.out.println('0' + j); Try(expression.substring(0, i) + ('0' + j) + expression.substring(i + 1), math_symbol, i + 1); } } } } } if (check_expression(expression, math_symbol)) { System.out.println(expression); not_found = false; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); while (n-- > 0) { String expression = sc.nextLine(); char[] math_symbols = {'+', '-', '*', '/'}; char math_symbol = expression.charAt(3); not_found = true; if (math_symbol == '?') { for (int i = 0; i < 4; i++) { math_symbol = math_symbols[i]; if (not_found) Try(expression.substring(0, 3) + math_symbol + expression.substring(4), math_symbol, 0); } } else { Try(expression, math_symbol, 0); } if (not_found) System.out.println("WRONG PROBLEM!"); } } }
Editor is loading...