本文主要是介绍42.蓝桥杯之警察智力训练,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
匪警请拨110,即使手机欠费也可拨通!
为了保障社会秩序,保护人民群众生命财产安全,警察叔叔需要与罪犯斗智斗勇,因而需要经常性地进行体力训练和智力训练!
某批警察叔叔正在进行智力训练:
1 2 3 4 5 6 7 8 9 = 110;
请看上边的算式,为了使等式成立,需要在数字间填入加号或者减号(可以不填,但不能填入其它符号)。之间没有填入符号的数字组合成一个数,
例如:12+34+56+7-8+9 就是一种合格的填法;123+4+5+67-89 是另一个可能的答案。
请你利用计算机的优势,帮助警察叔叔快速找到所有答案。
每个答案占一行。形如:12+34+56+7-8+9123+4+5+67-89…
已知的两个答案可以输出,但不计分。
各个答案的前后顺序不重要。
package exe41_45;import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
//time is to long
import java.util.Set;
public class Exe42 {static Set<String> set = new HashSet<String>();public static void main(String[] args) {char string[] = new char[]{'1',' ','2',' ','3',' ','4',' ','5',' ','6',' ','7',' ','8',' ','9'};char operator[] = new char[]{'+','-',' '};dfs(string,operator,1);print();}private static void print() {Iterator<String> iterator = set.iterator();while(iterator.hasNext()){System.out.println(iterator.next());}}private static void dfs(char[] string, char[] operator, int begin) {if (begin>=string.length) {check(string);return ;}for (int i = begin; i < string.length; i+=2) {for (int j = 0; j < operator.length; j++) {string[i] = operator[j];dfs(string, operator, begin+2);}}}private static void check(char[] string) {StringBuffer sb = new StringBuffer();String str = new String(string);str = str.replaceAll(" ", "");
// char ch[] = str.toCharArray();String[] strsql = str.split("[+-]");String[] operator = str.split("\\d+");Integer[] inter = new Integer[strsql.length];int sum = Integer.parseInt(strsql[0]);sb.append(sum);for (int i = 1; i < strsql.length; i++) {inter[i] = Integer.parseInt(strsql[i]);switch (operator[i].charAt(0)) {case '+':sum += inter[i]; break;case '-':sum -= inter[i]; break;}sb.append(operator[i]+inter[i]);}
// System.out.println();
// char flag = '/';
// int temp = 0;
// int temp2 = 0;
// for (int i = 0; i < ch.length; i++) {
// if (ch[i]>='0'&&ch[i]<='9') {
// temp = temp*10+(ch[i]-'0');
// continue;
// }
// switch (flag) {
// case '+':sum+=temp;temp = 0; break;
// case '-':sum-=temp;temp = 0; break;
// }
// flag = ch[i];
// temp2 = temp;
// }if (sum == 110) {set.add(sb.toString());}}}
package exe41_45;import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;public class Exe42_1 {static Set<String> set = new HashSet<String>();public static void main(String[] args) {char string[] = new char[]{'1',' ','2',' ','3',' ','4',' ','5',' ','6',' ','7',' ','8',' ','9'};char operator[] = new char[]{'+','-',' '};dfs(string,operator,1);print();}private static void print() {Iterator<String> iterator = set.iterator();while(iterator.hasNext()){System.out.println(iterator.next());}}private static void dfs(char[] string, char[] operator, int begin) {if (begin>=string.length) {check(string);return ;}for (int i = begin; i < string.length; i+=2) {for (int j = 0; j < operator.length; j++) {string[i] = operator[j];dfs(string, operator, begin+2);}}}private static void check(char[] string) {String str = new String(string);str = str.replaceAll(" ", "");char ch[] = str.toCharArray();int sum = 0;char flag = '/';int temp = 0;int temp2 = 0;for (int i = 0; i < ch.length; i++) {if (ch[i]>='0'&&ch[i]<='9') {temp = temp*10+(ch[i]-'0');continue;}switch (flag) {case '+':sum+=temp;temp = 0; break;case '-':sum-=temp;temp = 0; break;}flag = ch[i];temp2 = temp;}switch (flag) {case '+':sum+=temp;temp = 0; break;case '-':sum-=temp;temp = 0; break;}if (sum == 110) {System.out.println(str);}}}
public class C2 {public static void main(String[] args) {// TODO Auto-generated method stub//String string = "123+4+5+67-89";char a[] = {' ','+','-'};char num[] = {'1','2','3','4','5','6','7','8','9'};for(char x1:a)for(char x2:a)for(char x3:a)for(char x4:a)for(char x5:a)for(char x6:a)for(char x7:a)for(char x8:a){int result = 0;char s[] = {x1,x2,x3,x4,x5,x6,x7,x8};String string = "";for(int i = 0;i<= 7;i++){string = string+num[i]+s[i];}string = string+num[8];string= string.replaceAll(" ","");String jianString[] = string.split("-");int addResult[] = new int[jianString.length];addResult[0] = 0;String addString[] = jianString[0].split("[+]");for(int j = 0;j<addString.length;j++){
addResult[0] += Integer.parseInt(String.valueOf(addString[j]));}result = addResult[0];for(int i = 1;i<jianString.length;i++){
String num1[] = jianString[i].split("[+]");
result = result - Integer.parseInt(String.valueOf(num1[0]));for(int p = 1;p<num1.length;p++)result += Integer.parseInt(String.valueOf(num1[p]));
}if(result == 110)System.out.println(string)}
}
}
支付宝扫红包,让我能够买杯咖啡,继续创作,谢谢大家!
这篇关于42.蓝桥杯之警察智力训练的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!