【蓝桥杯】带分数

带分数

题目要求用一个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;
}
相关推荐
java修仙传8 小时前
力扣hot100:每日温度
算法·leetcode·职场和发展
咚咚王者8 小时前
人工智能之核心基础 机器学习 第十章 降维算法
人工智能·算法·机器学习
源代码•宸8 小时前
Golang语法进阶(Context)
开发语言·后端·算法·golang·context·withvalue·withcancel
源代码•宸8 小时前
Golang语法进阶(Sync、Select)
开发语言·经验分享·后端·算法·golang·select·pool
sali-tec8 小时前
C# 基于OpenCv的视觉工作流-章8-形态学
人工智能·深度学习·opencv·算法·计算机视觉
栈与堆8 小时前
数据结构篇(1) - 5000字细嗦什么是数组!!!
java·开发语言·数据结构·python·算法·leetcode·柔性数组
LightYoungLee9 小时前
算法(五)树 Trees V2
学习·算法·深度优先
enmouhuadou9 小时前
什么是I/Q信号?
算法·信息与通信
2301_8002561110 小时前
第九章:空间网络模型(空间网络查询、数据模型、Connected、with Recursive、pgRouting)
网络·数据库·算法·postgresql·oracle
逑之11 小时前
C语言笔记10:sizeof和strlen,指针与数组
c语言·笔记·算法