【蓝桥杯】带分数

带分数

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

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

#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;
}

相关推荐

  1. -带分数

    2023-12-05 20:28:05       43 阅读
  2. -带分数

    2023-12-05 20:28:05       27 阅读
  3. 【[ 2013 省 B] 带分数

    2023-12-05 20:28:05       41 阅读
  4. 贪心+

    2023-12-05 20:28:05       65 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-05 20:28:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-05 20:28:05       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-05 20:28:05       82 阅读
  4. Python语言-面向对象

    2023-12-05 20:28:05       91 阅读

热门阅读

  1. 【Android 线程】在子线程中更新UI

    2023-12-05 20:28:05       56 阅读
  2. spark学习一-------------------Spark算子最详细介绍

    2023-12-05 20:28:05       50 阅读
  3. 再探Docker:从Docker基础到跨服务器部署

    2023-12-05 20:28:05       34 阅读
  4. SSL证书认证对搜索引擎有影响吗?

    2023-12-05 20:28:05       64 阅读
  5. 如何判别使用的junit是4还是5

    2023-12-05 20:28:05       52 阅读
  6. 异常与junit

    2023-12-05 20:28:05       56 阅读
  7. CF 1901B Chip and Ribbon 学习笔记

    2023-12-05 20:28:05       60 阅读
  8. springcloud==ribbon

    2023-12-05 20:28:05       60 阅读
  9. 【光的波长和频率计算】

    2023-12-05 20:28:05       60 阅读
  10. prompt提示

    2023-12-05 20:28:05       47 阅读
  11. Linux C语言 32-网络编程之UDP例程

    2023-12-05 20:28:05       63 阅读