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++;
			}
		}
	}
}
相关推荐
UIU11414 小时前
P16237 [蓝桥杯 2026 省 B] 应急布线
数据结构·c++·算法·蓝桥杯
海绵宝宝转agent15 小时前
LeetCodehot100题解
java·笔记·学习·算法·leetcode·深度优先
海绵宝宝转agent2 天前
Leetcode二叉树算法个人理解
笔记·学习·算法·leetcode·深度优先
爱聊灵光的维C2 天前
C语言初阶:关于函数设计的详细讲解和注意事项
c语言·开发语言·青少年编程·蓝桥杯·学习方法
海绵宝宝转agent3 天前
Leetcode100+面经
java·笔记·学习·算法·leetcode·深度优先
mjhcsp4 天前
Code date creation v8.0 竞赛数据生成与对拍实战指南
算法·ai·深度优先·cdc
wdfk_prog4 天前
LWIP教程 40:从 `socket()` 到 `lwip_socket()`——DFS fd、SAL Socket 与 lwIP Backend
算法·深度优先
GG.WinGel5 天前
三元组中心问题
java·数据结构·算法·青少年编程·蓝桥杯
GG.WinGel6 天前
质数数目(蓝桥杯练习题)
java·数据结构·算法·青少年编程·蓝桥杯
Tsuki_tl7 天前
【优选算法 — 双指针】经典例题题解+算法总结
算法·蓝桥杯·力扣·刷题·双指针·1024程序员节·优选算法