zoukankan      html  css  js  c++  java
  • 蓝桥杯 带分数

    蓝桥杯 带分数

    题目描述

    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;
    }
    
  • 相关阅读:
    简单的javascript抽奖程序
    Linux 二层协议架构组织
    常用正则表达式总结
    很好的矩阵覆盖问题
    很好的求幂的题目
    不错的题目-n个数连接得到的最大值
    netstat命令介绍-要用熟
    一次完整的http事务
    Apache vs. Nginx
    Python学习-生成器
  • 原文地址:https://www.cnblogs.com/fsh001/p/14269548.html
Copyright © 2011-2022 走看看