【蓝桥杯】带分数

带分数

题目要求用一个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;
}
相关推荐
北顾笙9803 小时前
day26-数据结构力扣
数据结构·算法·leetcode
德索精密工业-胡工3 小时前
M12连接器的增强技术:在高电磁干扰的车间里它是如何“活下来”的?
算法
ZenosDoron3 小时前
函数形参传数组
java·jvm·算法
极客天成ScaleFlash3 小时前
极客天成 NVFile 存算融合解决方案
算法·数据挖掘
Reisentyan3 小时前
[杭电春季联赛5]1004 赛马
算法
雨墨✘3 小时前
基于比较的三种排序算法:插入排序、合并排序和快排序
数据结构·算法·排序算法
故事和你913 小时前
洛谷-数据结构1-2-二叉树1
开发语言·数据结构·c++·算法·leetcode·动态规划·图论
xu_wenming3 小时前
手写数字识别项目教程
网络·算法
_日拱一卒3 小时前
LeetCode:19删除链表的倒数第N个节点
算法·leetcode·链表
AIoT科技物语3 小时前
免费开源!50+算法,Java基于YOLO框架的视频AI识别算法平台,适配低空无人机巡检、摄像头安防场景
java·人工智能·算法·yolo·开源