• 蓝桥杯-带分数



    标题:带分数

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

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

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

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

    题目要求:
    从标准输入读入一个正整数N (N<1000*1000)
    程序输出该数字用数码1~9不重复不遗漏地组成带分数表示的全部种数。
    注意:不要求输出每个表示,只统计有多少表示法!


    例如:
    用户输入:
    100
    程序输出:
    11

    再例如:
    用户输入:
    105
    程序输出:
    6


    资源约定:
    峰值内存消耗 < 64M
    CPU消耗 < 3000ms


    请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

    所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。

    注意: main函数需要返回0
    注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
    注意: 所有依赖的函数必须明确地在源文件中 #include <xxx>, 不能通过工程设置而省略常用头文件。

    提交时,注意选择所期望的编译器类型。

     

    思路:全排列1-9,然后将1-9分割成两部分,第一次用的也是这个思路,不过实现方法比较复杂。

     

    #include<iostream>
    #include<string.h>
    #include<string>
    #include<algorithm>
    using namespace std;
    int main()
    {
        int m;
        cin >> m;
        char a[10] = {'1','2','3','4','5','6','7','8','9'};
        int sum = 0;
        string s,s1,s2,s3;
        int n1,n2,n3;
        do {
            s = "";
            for(int i=0; i<=8; i++) {
                
                s += a[i];
            }
    //        cout << s;
    //        cout << endl;
            // 截取的长度 
            for(int i=1; i<=7; i++) {
                for(int j=1; j<=8-i; j++) {
                    s1 = s.substr(0, i);
                    s2 = s.substr(i, j);
                    s3 = s.substr(i+j, 9-i-j);
                    n1 = atoi(s1.c_str());
                    n2 = atoi(s2.c_str());
                    n3 = atoi(s3.c_str());
    //                
                    if(n1+1.0*n2/n3 == m) {
                        sum++;
    //                    cout << n1 << "  " << n2 << "  " << n3 << endl;
                    }
                }
            }
        } while(next_permutation(a, a+9));
        cout << sum;
    }

     

    AC代码:

    #include<iostream>
    #include<string.h>
    #include<string>
    #include<algorithm>
    using namespace std;
    int main()
    {
        int m;
        cin >> m;
        int a[10] = {1,2,3,4,5,6,7,8,9};
        int sum = 0;
        long long n1,n2,n3;
        do { 
            
            for(int i=1; i<=7; i++) {
                for(int j=1; j<=8-i; j++) {
                    // left -> i 位, up -> j 位, down, 
                    n1 = n2 = n3 = 0;
                    int x;
                    int q=0;
                    for(x=0; x<i; x++) {
                        n1 = n1*10 + a[q];
                        q++;
                    }
                    for(x=0; x<j; x++) {
                        n2 = n2*10 + a[q];
                        q++;
                    }
                    for(x=0; x<=8-i-j; x++) {
                        n3 = n3*10 + a[q];
                        q++;
                    } 
    //                cout << n1 << " " << n2 << " " << n3 << endl;
                    if(n1+1.0*n2/n3 == m) {
                        sum++;
                    }
                }
            }
        } while(next_permutation(a, a+9));
        cout << sum;
    }

     


  • 相关阅读:
    pecl install swoole 安装php扩展swoole
    RBAC
    mysql数据类型
    ES6——Proxy的this问题
    ES6——Proxy实现链式操作
    ES6——Symbol内置值
    ES6——yield例子
    ES6——Generator的next()方法
    ES6——Thunkify用法
    Linux学习day1
  • 原文地址:https://www.cnblogs.com/wzy-blogs/p/10321092.html
Copyright © 2020-2023  润新知