【蓝桥杯】带分数

带分数

题目要求用一个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;
}
相关推荐
白白白小纯5 分钟前
算法篇—返回倒数第k个节点
c语言·数据结构·算法·leetcode
小羊先生car15 分钟前
RTOS-F429-HAL-(动/静态)任务的创建(2026/7/27)
开发语言·算法·c#
无忧.芙桃22 分钟前
数据结构之堆
c语言·数据结构·c++·算法·
小蒋学算法41 分钟前
算法-删除元素后最大固定点数目-典型最长增长序列算法
数据结构·算法
RD_daoyi1 小时前
Google偷偷给AI引用加了五个新功能:内联引用、悬停预览、品牌展示……但点击率真的回来了吗?
大数据·网络·人工智能·算法·安全·搜索引擎
船漏了就会沉1 小时前
后缀自动机(SAM):字符串处理的“万能瑞士军刀”
算法
白白白小纯2 小时前
算法篇—链表的中间节点
c语言·数据结构·算法·leetcode
Frostnova丶2 小时前
(19)LeetCode 54. 螺旋矩阵
算法·leetcode·矩阵
Frostnova丶2 小时前
(18)LeetCode 73. 矩阵置零
算法·leetcode·矩阵
冻柠檬飞冰走茶3 小时前
PTA基础编程题目集 7-11 分段计算居民水费(C语言实现)
c语言·开发语言·算法