NO5---蓝桥杯---带分数---dfs---全排列

问题描述
100 可以表示为带分数的形式:100 = 3 + 69258 / 714。

还可以表示为:100 = 82 + 3546 / 197。

注意特征:带分数中,数字1~9分别出现且只出现一次(不包含0)。

类似这样的带分数,100 有 11 种表示法。

输入格式
从标准输入读入一个正整数N (N<1000*1000)

输出格式
程序输出该数字用数码1~9不重复不遗漏地组成带分数表示的全部种数。

注意:不要求输出每个表示,只统计有多少表示法!

样例输入1
100
样例输出1
11
样例输入2
105
样例输出2
6

主要就是理解 像奥数题   活用dfs来全排列
package test_01;
import java.util.Scanner;

public class Main {
	public static int cnt=0,num=0;
	public static boolean bool[]=new boolean[10];
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
        num=sc.nextInt();
        sc.close();
        for(int i=1;i<=7;i++)
        	for(int j=1;j<=7;j++)
        		if(9-i-j>=1)
        		{
        			dfs(0,0,0,i,j,9-i-j);
        		}
        System.out.println(cnt);
        
	}
	public static void dfs(int a,int b,int c,int alen,int blen,int clen)
	{
		if(alen==0&&blen==0&&clen==0)
		{
			if(b%c==0&&b/c==num-a)
			{
				cnt++;
			}
			return;
		}
		for(int i=1;i<10;i++)
		{
			if(!bool[i])
			{
				bool[i]=true;
				if(alen>0)
					dfs(a*10+i,b,c,alen-1,blen,clen);
				else if(blen>0)
					dfs(a,b*10+i,c,alen,blen-1,clen);
				else if(clen>0)
					dfs(a,b,c*10+i,alen,blen,clen-1);
				bool[i]=false;
			}
		}
	}
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东箭武

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值