【蓝桥杯】历届试题 带分数

问题描述

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

1-9进行全排列,再进行分段,分为三段,将三段进行运算。

import java.util.Scanner;

public class Main {
	static int N=10;
	static int[] num=new int[N];
	static boolean[] used=new boolean[N];
	static int target;
	static int cnt=0;
	
	static int calc(int l,int r) {
		int res=0;
		for(int i=l;i<=r;i++)
			res=res*10+num[i];
		return res;
	}
	
	static void dfs(int u) {
		if (u==9) {
			 for(int i = 0; i < 7; i++)
		            for(int j = i + 1; j < 8; j++){
		                int a = calc(0, i);
		                int b = calc(i + 1, j);
		                int c = calc(j + 1, 8);
		                if(a * c + b == c * target) 
		                	cnt++;
		            }
		        return;
		}
		//全排列
		for(int i=1;i<=9;i++) {
			if(!used[i]) {
				used[i]=true;
				num[u]=i;
				dfs(u+1);
				used[i]=false;
			}
		}
	}
	
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		target=in.nextInt();
		dfs(0);
		System.out.println(cnt);
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

蒙面侠1024

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

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

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

打赏作者

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

抵扣说明:

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

余额充值