蓝桥杯历届试题 带分数【枚举】

问题描述

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

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

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

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

输入格式

从标准输入读入一个正整数N (N<1000*1000)

输出格式

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

注意:不要求输出每个表示,只统计有多少表示法!

样例输入1

100

样例输出1

11

样例输入2

105

样例输出2

6

思路:搜索或者枚举都可以做这道题,通过枚举整数部分和分母,可以求出来分子,然后我们就判断当前是否满足题的条件,

如果满足次数加一。

#include<set>
#include<map>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 5000;
bool isok(int a, int b, int c)
{   //统计abc 中每位数出现的次数
    bool f[12] = {false};
    while(a)
    {
        int tmp = a % 10;
        if(f[tmp] == true)
            return false;
        f[tmp] = true;
        a /= 10;
    }
    while(b)
    {
        int tmp = b % 10;
        if(f[tmp] == true)
            return false;
        f[tmp] = true;
        b /= 10;
    }
    while(c)
    {
        int tmp = c % 10;
        if(f[tmp] == true)
            return false;
        f[tmp] = true;
        c /= 10;
    }
    if(f[0] == true)  //如果有0出现
        return false;
    for(int i = 1; i <= 9; ++i)
        if(!f[i])
            return false;
    return true;
}

int main()
{
    int n;
    cin >> n;
    int cnt = 0;
    for(int i = 1; i < n; ++i)
    {
        for(int k = 1; k < 1e4; ++k) //分母
        {
            int j = (n - i) * k; //求出来分子
            if(i != k && j >= k)
            {
                if(isok(i, j, k))
                    cnt++;
            }
        }
    }
    cout << cnt << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41785863/article/details/88768920
今日推荐