zoukankan      html  css  js  c++  java
  • Java实现蓝桥杯凑算式(全排列)

    题目6、凑算式

    凑算式

     B      DEF
    

    A + — + ------- = 10
    C GHI

    (如果显示有问题,可以参见【图1.jpg】)

    这个算式中AI代表19的数字,不同的字母代表不同的数字。

    比如:
    6+8/3+952/714 就是一种解法,
    5+3/1+972/486 是另一种解法。

    这个算式一共有多少种解法?

    注意:你提交应该是个整数,不要填写任何多余的内容或说明性文字。

    (碰到除法问题,要特别注意,如题目未事先声明进行整除,均首先消除分母,进行乘法运算,这样可以消除浮点数误差)
    在这里插入图片描述
    结果:29

    package 第二次线下模拟;
    
    import java.util.ArrayList;
    
    public class 凑算式 {
    	public static int count = 0;
    	public static ArrayList<Integer> list = new ArrayList<Integer>();
    
    	public static void main(String[] args) {
    		f();
    		System.out.println(count);
    	}
    
    	public static void f() {
    		if (list.size() >= 9) { 
    			int a1 = list.get(0) * list.get(2) * (list.get(6) * 100 + list.get(7) * 10 + list.get(8));
    			int a2 = list.get(1) * (list.get(6) * 100 + list.get(7) * 10 + list.get(8));
    			int a3 = (list.get(3) * 100 + list.get(4) * 10 + list.get(5)) * list.get(2);
    			if (a1 + a2 + a3 == 10 * list.get(2) * (list.get(6) * 100 + list.get(7) * 10 + list.get(8))) {
    				count++;
    				
    			}
    			return;
    		}
    		for (int i = 1; i <= 9; i++) {
    			if (!list.contains(i)) {
    				list.add(i);
    				f();
    				list.remove(list.size() - 1);
    			}
    		}
    	}
    }
    
    
  • 相关阅读:
    python-Beautiful rose
    python-and和 or用法
    myspl数据库基础
    python 协程
    python-os 模块
    python-logging模块
    异常处理
    面向对象-类中的三个装饰器
    Flask初见
    django中的ContentType使用
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12946332.html
Copyright © 2011-2022 走看看