【蓝桥杯】带分数

带分数

题目要求用一个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;
}
相关推荐
Cx330❀8 分钟前
【数据结构初阶】--排序(四):归并排序
c语言·开发语言·数据结构·算法·排序算法
余_弦30 分钟前
区块链中的密码学 —— 密钥派生算法
算法·区块链
亲爱的非洲野猪1 小时前
令牌桶(Token Bucket)和漏桶(Leaky Bucket)细节对比
网络·算法·限流·服务
NAGNIP1 小时前
一文读懂LLAMA
算法
烧冻鸡翅QAQ1 小时前
62.不同路径
算法·动态规划
番薯大佬1 小时前
编程算法实例-冒泡排序
数据结构·算法·排序算法
queenlll1 小时前
P2404 自然数的拆分问题(典型的dfs)
算法·深度优先
wydaicls1 小时前
用函数实现方程函数解题
人工智能·算法·机器学习
·白小白1 小时前
力扣(LeetCode) ——100. 相同的树(C语言)
c语言·算法·leetcode
CoovallyAIHub2 小时前
为什么85%的企业AI项目都失败了?
深度学习·算法·计算机视觉