zoukankan      html  css  js  c++  java
  • Java实现 蓝桥杯 猜算式

    猜算式

    看下面的算式:
    □□ x □□ = □□ x □□□
    它表示:两个两位数相乘等于一个两位数乘以一个三位数。
    如果没有限定条件,这样的例子很多。
    但目前的限定是:这9个方块,表示1~9的9个数字,不包含0。
    该算式中1至9的每个数字出现且只出现一次!
    比如:
    46 x 79 = 23 x 158
    54 x 69 = 27 x 138
    54 x 93 = 27 x 186

    请编程,输出所有可能的情况!
    注意:
    左边的两个乘数交换算同一方案,不要重复输出!
    不同方案的输出顺序不重要

    建议用:全排列解决问题
    十一种

    如果出来22种的话,可能是重复了,加一个a!=b

    package 第二次模拟;
    
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class Demo5算式 {
    	public static int  count=0;
    	public static void main(String[] args) {
    	 
    		dfs(new ArrayList<Integer>());
    		System.out.println(count);
    		
    	}
    	public static void dfs(ArrayList<Integer> list){
    		if(list.size()==9){
    			int a =list.get(0)*10+list.get(1);
    			int b =list.get(2)*10+list.get(3);
    			int c = list.get(4)*10+list.get(5);
    			int d = list.get(6)*100+list.get(7)*10+list.get(8);
    			
    			if (a*b==c*d && a<b) {
    				count++;
    			}
    			
    			return ;
    		}
    		for (int i = 1; i <=9; i++) {
    			if (!list.contains(i)) {
    				list.add(i);
    				dfs(list);
    				list.remove(list.size()-1);
    			}
    		}
    	}
    
    }
    
    
  • 相关阅读:
    vue
    vue
    vue
    vue
    vue
    vue
    vue
    vue
    vue
    vue
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13076048.html
Copyright © 2011-2022 走看看