蓝桥杯-带分数

法一 

/*
再每一个a里去找c,他们共用一个st数组,可以解决重复出现数字
通过ac确定b,b不能出现<=0 b出现的数不能和ac重复
 */

import java.util.Scanner;

public class Main {
    static int n,res;
    static boolean[] st = new boolean[15];
    static boolean[] backup = new boolean[15];
    static int[] data = new int[15];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        dfs_a(1,0);
        System.out.println(res);
    }
    public static void dfs_a(int u,int a){
        if (a > n) return;
        
        //每一个a里去找不出现a中数字的c的全排列
        if (a > 0)
            dfs_c(u,a,0);

        for(int i = 1;i <= 9;i++){
            if (!st[i]){
                st[i] = true;
                dfs_a(u + 1,a * 10 + i);
                st[i] = false;
            }
        }
    }

    public static void dfs_c(int u,int a,int c){
        if (u == 9) return;

        if (c > 0)
            //通过ac确定b
            if (check(a,c)) res++;
        for(int i = 1;i <= 9;i++){
            if (!st[i]){
                st[i] = true;
                dfs_c(u + 1,a,c * 10 + i);
                st[i] = false;
            }
        }
    }

    public static boolean check(int a,int c){
        //n 和 c 可能会溢出
        long b = n * (long)c - a * c;
        if (b <= 0) return false;
        backup = st.clone();
        while (b > 0){
            
            //b的每一位不能再之前出现过,并且不能等于0
            int ge = (int)b % 10;
            b /= 10;
            if (backup[ge] || ge == 0) return false;
            backup[ge] = true;  //记得出现出的数字也要标记一下
        }
        for(int i = 1;i <= 9;i++){
            if (!backup[i]) return false;
        }
        return true;
    }
}

法二

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    static int n;// 目标数
    static int[] a  = new int[10];// 全排列数组
    static boolean visi[] = new boolean[10]; // 放入数组就进行记录,避免重复使用
    static int ans = 0;// 统计
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        dfs(1);
        System.out.println(ans);
    }
    
    //枚举全排列
    public static void dfs(int u)
    {
        if(u == 10)
        {
            check();
            return;
        }
        for (int i = 1; i <= 9; i++)
        {
            if(!visi[i])
            {
                visi[i] = true;
                a[u] = i;
                dfs(u + 1);
                // 回溯
                visi[i] = false;
            }
        }
    }
    
    //切割成三份是否满足题目给定式子
    public static void check()
    {
        for(int i = 1 ; i <= 7;i++){
            
            //a最多可以取七位,取前七位即可,全排列有对称关系
            int num1 = Test(1, i);//枚举num1所有可能得长度
            //num1再继续加下面num2与num3也是超标已经超过了N,没有必要再继续
            if(num1 >= n)continue;
            for(int j = i + 1 ; j <= 8;j++){
                int num2 = Test(i + 1,j);//分子数
                int num3 = Test(j + 1, 9);//分母数
                if(num2 % num3 == 0 && num1 + num2 / num3 == n)
                {//进行判断
                    ans++;
                }
            }
        }
    }
    
    
    public static int Test(int start,int end)
    {
        int number1 = 0;
        for (int i = start; i <= end; i++)
        {
            number1 = number1 * 10 + a[i];
        }
        return number1;
    }
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值