蓝桥杯 带分数

蓝桥杯 带分数

题目描述

100 可以表示为带分数的形式:100=3+69258/714

还可以表示为:100=82+3546/197

注意特征:带分数中,数字 1∼9
分别出现且只出现一次(不包含 0)。

类似这样的带分数,100有 11 种表示法。

输入格式

一个正整数。

输出格式

输出输入数字用数码 1∼9 不重复不遗漏地组成带分数表示的全部种数。

数据范围

1≤N<10^6

输入样例1
100
输出样例1
11
输入样例2
105
输出样例2
6
题目思路

做法一:暴力枚举每一种数字组合,将数字组合枚举分成三个数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int n,ways[10],cnt = 0;
int a,b,c;
bool used[10];

int fun(int st,int ed)
{
    int r = 0;
    while(st<=ed)
    {
        r *= 10;
        r += ways[st++];
    }
    return r;
}

void dfs(int u)
{
    if(u>9)
    {
        for(int i=1;i<=7;i++)
        {
            a = fun(1,i);
            for(int j=i+1;j<=8;j++)
            {
                b = fun(i+1,j);
                c = fun(j+1,9);
                if(n*c==c*a+b)
                    cnt++;
            }
        }
        return ;
    }
    
    for(int i=1;i<=9;i++)
    {
        if(!used[i])
        {
            used[i] = true;
            ways[u] = i;
            
            dfs(u + 1);
            
            used[i] = false;
            ways[u] = 0;
        }
    }
}

int main()
{
    cin >> n;
    
    dfs(1);
    cout << cnt;
    
    return 0;
}

做法二:直接对a进行枚举,然后对c进行枚举

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 10;

int n;
bool st[N], backup[N];
int ans;

bool check(int a, int c)
{
    long long b = n * (long long)c - a * c;

    if (!a || !b || !c) return false;

    memcpy(backup, st, sizeof st);
    while (b)
    {
        int x = b % 10;     // 取个位
        b /= 10;    // 个位删掉
        if (!x || backup[x]) return false;
        backup[x] = true;
    }

    for (int i = 1; i <= 9; i ++ )
        if (!backup[i])
            return false;

    return true;
}

void dfs_c(int u, int a, int c)
{
    if (u > 9) return;

    if (check(a, c)) ans ++ ;

    for (int i = 1; i <= 9; i ++ )
        if (!st[i])
        {
            st[i] = true;
            dfs_c(u + 1, a, c * 10 + i);
            st[i] = false;
        }
}

void dfs_a(int u, int a)
{
    if (a >= n) return;
    if (a) dfs_c(u, a, 0);

    for (int i = 1; i <= 9; i ++ )
        if (!st[i])
        {
            st[i] = true;
            dfs_a(u + 1, a * 10 + i);
            st[i] = false;
        }
}

int main()
{
    cin >> n;

    dfs_a(0, 0);

    cout << ans << endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wynpz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值