第七届蓝桥杯JAVA B组真题解析-凑算式(第三题)

第七届蓝桥杯JAVA B组真题解析-凑算式(第三题)

凑算式

A+B/C+DEF/GHI =10

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

这个算式中A~I代表1~9的数字,不同的字母代表不同的数字。

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

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

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

凑算式

思考思路:

  1. 数字不能重复

  2. 做除法要使用double类型

    第一种方法 穷举法(写了很多for)
    代码很丑 但是思路简单 清晰 其实可以用递归写的,但是没有想出来,还请高手指教

public class Demo3
{
    public static void main(String[] args)
    {
        int [] number = {1,2,3,4,5,6,7,8,9};
        double a,b,c,d,e,f,g,h,i;
        long times = 0L;
        int sucess = 0;
        a = b = c = d = e = f = g = h = i =0.0d;
        for (int j1 = 0; j1 < number.length; j1++)
        {
            a = number[j1];
            for(int j2=0;j2 < number.length; j2++)
            {
                b = number[j2];
                if(a!=b)
                {
                    for(int j3=0;j3 < number.length; j3++)
                    {
                        c = number[j3];
                        if(c!=a&&c!=b)
                        {
                            for(int j4=0;j4 < number.length; j4++)
                            {
                                d = number[j4];
                                if(d!=a&&d!=b&&d!=c)
                                {
                                    for(int j5=0;j5 < number.length; j5++)
                                    {
                                        e = number[j5];
                                        if(e!=a&&e!=b&&e!=c&&e!=d)
                                        {
                                            for(int j6=0;j6 < number.length; j6++)
                                            {
                                                f = number[j6];
                                                if(f!=a&&f!=b&&f!=c&&f!=d&&f!=e)
                                                {
                                                    for(int j7=0;j7 < number.length; j7++)
                                                    {
                                                        g = number[j7];
                                                        if(g!=a&&g!=b&&g!=c&&g!=d&&g!=e&&g!=f)
                                                        {
                                                            for(int j8=0;j8 < number.length; j8++)
                                                            {
                                                                h = number[j8];
                                                                if(h!=a&&h!=b&&h!=c&&h!=d&&h!=e&&h!=f&&h!=g)
                                                                {
                                                                    for(int j9=0;j9 < number.length; j9++)
                                                                    {

                                                                        i = number[j9];
                                                                        if(i!=a&&i!=b&&i!=c&&i!=d&&i!=e&&i!=f&&i!=g&&i!=h)
                                                                        {
                                                                            times++;
                                                                            if(a+b/c+((d*100+e*10+f)/(g*100+h*10+i))==10.0d)
                                                                            {
                                                                                sucess++;
                                                                                System.out.println("a="+a);
                                                                                System.out.println("b="+b);
                                                                                System.out.println("c="+c);
                                                                                System.out.println("d="+d);
                                                                                System.out.println("e="+e);
                                                                                System.out.println("f="+f);
                                                                                System.out.println("g="+g);
                                                                                System.out.println("h="+h);
                                                                                System.out.println("i="+i);
                                                                                System.out.println("----------------------------");
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        System.out.println("共运行"+times+"次"+","+"共有"+sucess+"解");
    }

第二种是别人博客上的代码 我找过来的 用的是递归 看了好久也没懂 有看懂的朋友麻烦讲解一下谢谢啦
毕竟算法渣渣 等我有空就去注明出处

———————————下面是别人的代码———————————————


public class SimpleDemo3
{
    static int[] s = new int[9];
    static int[] v = new int[9];
    static int sum = 0;

    public static void main(String[] args)
    {
        s(0);
        System.out.println(sum);
    }

    public static void s(int code)
    {
        if (code == 9)
        {
            int a = s[0], b = s[1], c = s[2], def = s[3] * 100 + s[4] * 10 + s[5], ghi = s[6] * 100 + s[7] * 10 + s[8];
            if (c * ghi * (10 - a) == b * ghi + c * def)
            {
                sum++;
            }
        }
        for (int i = 0; i < 9; i++)
        {
            if (v[i] == 0)
            {
                v[i] = 1;
                s[code] = i + 1;
                s(code + 1);
                v[i] = 0;// 回溯
            }
        }
    }
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值