蓝桥杯-带分数

100 可以表示为带分数的形式:100 = 3 + 69258/714
还可以表示为:100 = 82 + 3546 / 197
注意特征:带分数中,数字 1∼9 分别出现且只出现一次(不包含 0)。

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

输入格式

一个正整数。

输出格式

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

数据范围

1≤N<1e6

输入样例1:

100

输出样例1:

11

输入样例2:

105

输出样例2:

6

题解:

  • 枚举所有 a 的情况, 在每种 a 的前提下, 再枚举所有 c 的情况
  • 根据 a, c, n 计算出 b
  • 判断是否满足 [1, 9] 仅出现过一次, 且 a, b, c中不含有0

代码并不是很长, 去除空行大概有五十行, 大家耐心看, 代码中有很多注释, 如果有不理解的地方可以再问~

#include <bits/stdc++.h>
using namespace std;
const int N = 20;
int n, ans;
bool st[N], back[N];

bool check(int a, int c)
{
    int b = n * c - a * c;  // 公式: n = a + b / c  ==> b = n * c - a * c

    if (!a || !b || !c) return false;  //  会把 a b c 任意一个等于 0 的情况给筛掉

    memcpy(back, st, sizeof st);    // 赋值st的状态, 用于判断 [1, 9] 是否全出现过
    while (b)
    {
        int x = b % 10;     // 取个位
        b /= 10;    // 个位删掉
        if (!x || back[x]) return false;  // 保证 b 中不含 0, 同时 保证 [1, 9] 只出现过一次
        back[x] = true;
    }
    for (int i = 1; i <= 9; i ++) if (back[i] == false) return false;
    return true;
}

void dfs_c(int u, int a, int c)
{
    if (check(a, c)) ans ++;  //  这里同样可以不用管 c 是否为 0, check函数中有 处理 c 等于 0 的情况

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

void dfs_a(int u, int a)  // 枚举 a 的所有可以能 (1, 12, 123, ... 987654321)
{
    if (a > n) return;   // 剪枝, a > n的时候 b,c没有满足条件的值 (没有这个也不会死循环, 有这个可以减少代码运行的时间)

    if(a != 0) dfs_c(u, a, 0);  // 这和判断可以写, 也可以不写, 写的话运行时间会快一些, 不写的话在check函数中对 等于 0 进行的处理
    
    for (int i = 1; i <= 9; i ++)  
    {
        if (st[i]) continue;
        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;
}

觉得写的不错的话, 点个赞吧~

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,带分数可以用分数类来表示。下面是一个简单的带分数类的实现: ```java public class Fraction { private int integer; private int numerator; private int denominator; public Fraction(int integer, int numerator, int denominator) { this.integer = integer; this.numerator = numerator; this.denominator = denominator; simplify(); } private void simplify() { if (numerator < 0 && denominator < 0) { numerator = -numerator; denominator = -denominator; } if (denominator < 0) { numerator = -numerator; denominator = -denominator; } if (integer < 0 && numerator > 0) { numerator = -numerator; } if (integer < 0 && numerator == 0) { integer = -integer; } if (numerator >= denominator) { integer += numerator / denominator; numerator = numerator % denominator; } int gcd = gcd(numerator, denominator); numerator /= gcd; denominator /= gcd; } private int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } public Fraction add(Fraction other) { int newNumerator = this.numerator * other.denominator + other.numerator * this.denominator; int newDenominator = this.denominator * other.denominator; int newInteger = this.integer + other.integer; return new Fraction(newInteger, newNumerator, newDenominator); } public String toString() { if (integer == 0 && numerator == 0) { return "0"; } String result = ""; if (integer != 0) { result += integer; if (numerator != 0) { result += "_"; } } if (numerator != 0) { result += numerator + "/" + denominator; } return result; } } ``` 这个带分数类实现了以下功能: - 构造函数可以根据整数部分、分子和分母创建一个带分数对象。 - simplify() 方法可以将带分数对象化简,如将负号移到分子上、将整数部分和真分数部分合并、将分数化简等。 - add() 方法可以将两个带分数对象相加,返回一个新的带分数对象。 - toString() 方法可以将带分数对象转换为字符串形式。 这个类实现了带分数的加法操作,可以参考这个类来实现其他的运算操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值