蓝桥杯 15省赛 B3 三羊献瑞(暴力破解)

蓝桥杯 15省赛 B3 三羊献瑞(暴力破解)

三羊献瑞
观察下面的加法算式:
其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。
在这里插入图片描述

思路:
只要可以暴力破解(数据规模不是很大),代码又臭又长但又何妨

public class 细节_3 {

	public static void main(String[] args) {
		
		//外8层的循环变量必须为0,不然没结果
		for(int a =0 ;a <10 ;a ++) {
			for(int b =0 ;b <10 ;b ++) {
				for(int c =0 ;c <10 ;c ++) {
					for(int d =0 ;d <10 ;d ++) {
						for(int e =0 ;e <10 ;e ++) {
							for(int f =0 ;f <10 ;f ++) {
								for(int g =0 ;g <10 ;g ++) {
									for(int h =0 ;h <10 ;h ++) {
										
										int[] arr = {a ,b ,c ,d ,e ,f ,g ,h};
										boolean[] flag =new boolean[10];
										
										for(int i :arr) {
											flag[i] =!flag[i];
										}
										int temp =0;
										for(boolean bool :flag) {
											if(!bool) {
												temp ++;
											}
										}
										if(temp !=2) {
											continue;
										}
										
										//每个数开头不能是'0'
										if(arr[4] ==0 ||arr[0] ==0) {
											continue;
										}
										
										int y =Integer.parseInt(arr[4]+""+arr[5]+""+arr[6]+""+arr[1]);
										int z =Integer.parseInt(arr[4]+""+arr[5]+""+arr[2]+""+arr[1]+""+arr[7]);
										int x =Integer.parseInt(arr[0]+""+arr[1]+""+arr[2]+""+arr[3]);
										if(x +y !=z) {
											continue;
										}										
										System.out.println("x"+x+"y"+y+"z"+z);
									}
								}
							}
						}
					}
				}		
			}
		}
	
		
			
	}

}

猜你喜欢

转载自blog.csdn.net/weixin_43638238/article/details/107667356