【蓝桥杯】带分数

带分数

题目要求用一个a+b/c的形式得到一个值,而且只能在1~9里面不重复的组合。

可以对1~9进行全排列,然后不断划分区间。

cpp 复制代码
#include<iostream>
#include<vector>
using namespace std;
int st[15];
int num[15];
int res;
int n;

int calc(int l, int r)
{
    int res = 0;
    for (int i = l; i <= r; i++)
    {
        res = res * 10 + num[i];
    }
    return res;
}

void dfs(int u)
{
    //分解
    if (u == 10)
    {
        for (int i = 1; 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, 9);
                if (a * c + b == n * c)
                {
                    res++;
                }
            }
        }
    }

    //组合1~9的全排列
    for (int i = 1; i <= 9; i++)
    {
        if (!st[i])
        {
            //如果没有排列过
            num[u] = i;
            st[i] = true;
            dfs(u + 1);
            st[i] = false;
            num[u] = -1;
        }
    }
}

int main(void)
{
    cin >> n;
    dfs(1);
    printf("%d", res);
    return 0;
}
相关推荐
玛卡巴卡ldf1 分钟前
【LeetCode 手撕算法】(矩阵)73-矩阵置零、54-螺旋矩阵(贪吃蛇)、48-旋转图像
java·数据结构·算法·leetcode·力扣
C^h1 分钟前
RTthread中的内存池理解
linux·数据库·c++·算法·嵌入式
深藏功yu名1 分钟前
Day25(高阶篇):RAG检索与重排序算法精研|从原理到参数调优,彻底攻克检索瓶颈
人工智能·算法·ai·自然语言处理·排序算法·agent
郝学胜-神的一滴6 分钟前
深入解析:生成器在UserList中的应用与Python可迭代对象实现原理
开发语言·python·程序人生·算法
雪木木6 分钟前
刷题:力扣热题100--滑动窗口(Day03)
算法·leetcode
lcj25118 分钟前
蓝桥杯C++:数据结构(功能导向速查)
数据结构·c++·蓝桥杯
Yzzz-F9 分钟前
Problem - 2157D - Codeforces
算法
颜酱12 分钟前
回溯算法实战练习(2)
javascript·后端·算法
We་ct18 分钟前
LeetCode 153. 旋转排序数组找最小值:二分最优思路
前端·算法·leetcode·typescript·二分·数组
享哥24 分钟前
tick 数据探索笔记:从抓取到理解
算法