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++;
			}
		}
	}
}
相关推荐
有点。6 小时前
C++深度优先搜索(二)
开发语言·c++·深度优先
Keven_111 天前
算法札记:二叉树前序、中序、后序遍历及对应序列重要性质
数据结构·算法·深度优先
依然鸣1 天前
蓝桥杯多校模拟赛 题解
数据结构·c++·经验分享·学习·算法·蓝桥杯
小星星闪亮登场2 天前
2026萌新联赛第三场-- (郑州轻工业大学)
数据结构·c++·经验分享·算法·贪心算法·排序算法·深度优先
山峰哥2 天前
数据库工程与SQL调优:从慢查询到秒级响应的实战之路
java·开发语言·数据库·sql·深度优先·启发式算法
木井巳3 天前
【DFS解决floodfill算法】岛屿的最大面积
java·算法·leetcode·深度优先
Tisfy3 天前
LeetCode 1406.石子游戏 III:递归(DFS+记忆化) / 递推(DP+原地滚动)
leetcode·游戏·深度优先·dfs·题解·博弈
Tisfy5 天前
LeetCode 0486.预测赢家:深度优先搜索(DFS)
算法·leetcode·深度优先·dfs·博弈
zander2585 天前
LeetCode 79. 单词搜索
算法·深度优先
zander2586 天前
LeetCode 17. 电话号码的字母组合
算法·深度优先