P8599 [蓝桥杯 2013 省 B] 带分数(dfs+全排列+断点判断)

思路:1.深度枚举所有排列情况

2.设置为每个排列设置两个断点,分为三部分:a,b,c

3.转换为乘法判断条件,满足加一

代码如下:(可用next_permutation全排列函数代替dfs)

复制代码
#include<iostream>
#include<stdio.h>
using namespace std;
int ans = 0;

int visit[11] = { 0 };

int arr[11] = { 0 }, k = 0;

int tonum(int l, int r) {//将num数组的第l至r个数转为一个数
	int re = 0;
	for (int i = l; i <r; i++) {
		re = re * 10 + arr[i];
	}
	return re;
}

void dec();

void dfs();

int l = 0;

int main()
{
	
	scanf("%d", &l);
	dfs();
	cout << ans << endl;
	return 0;
}
void dfs()
{
	for (int i = 1; i <= 9; i++)
	{

		if (!visit[i])
		{
			visit[i] = 1;
			arr[k++] = i;
			dfs();
			if (k == 9)		dec();
			k--;
			visit[i] = 0;
		}
	}
}

void dec()
{
	for (int i = 1; i <= 7; i++)
	{	int a = tonum(0, i);
		for (int j = i; j <= 8; j++)
		{
			int b = tonum(i, j);
			int c = tonum(j, 9);
			if ((l - a) * c == b) {
				ans++;
			}
		}
	}
}
相关推荐
bybitq1 小时前
LeetCode236-二叉树的最近公共祖先(LCA)问题详解-C++
算法·深度优先
a程序小傲6 小时前
听说前端又死了?
开发语言·前端·mysql·算法·postgresql·深度优先
FMRbpm10 小时前
树的练习7--------LCR 052.递增顺序搜索树
数据结构·c++·算法·leetcode·深度优先·新手入门
不穿格子的程序员12 小时前
从零开始写算法——回溯篇3:括号生成 + 单词搜索
算法·深度优先·回溯
bybitq13 小时前
Leetcode-124-二叉树最大路径和-Python
算法·leetcode·深度优先
鱼跃鹰飞13 小时前
Leetcode会员专享题:426.将二叉搜索树转换为排序的双向链表
数据结构·算法·leetcode·链表·深度优先
(❁´◡`❁)Jimmy(❁´◡`❁)1 天前
Atcoder abc441A~F 题解
算法·深度优先·图论
氷泠1 天前
路径总和系列(LeetCode 112 & 113 & 437 & 666)
leetcode·前缀和·深度优先·路径总和
程序员-King.1 天前
day154—回溯—分割回文串(LeetCode-131)
算法·leetcode·深度优先·回溯
程序员-King.1 天前
day152—回溯—电话号码的字母组合(LeetCode-17)
算法·leetcode·深度优先·递归