蓝桥杯PREV-3 带分数 试题解析

试题 历届试题 带分数

提交此题  

资源限制

时间限制:1.0s   内存限制:256.0MB

问题描述

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 <iostream>

#include <sstream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

string tostring(int a){
    ostringstream os;
    os<<a;
    return os.str();

}

int IntLen(int a){
    string sa=tostring(a);
    int alen=sa.size();
    return alen;
}


bool IsOneOccur(long a,long b){
    string sa=tostring(a);
    string sb=tostring(b);
    long alen=sa.size();
    long blen=sb.size();
    
    bool IsOne=true;
    for(int i=0;i<alen;i++){
        for(int j=0;j<blen;j++){
            if(sa[i]-sb[j]==0){
                IsOne=false;
                break;
            }
        }
    } 
    return IsOne;
}

bool IsDiff(long a){
    string sa=tostring(a);
    int alen=sa.size();
    
    bool isDf=true;
    for(int i=0;i<alen;i++){
        if(sa[i]=='0'){
            isDf=false;
            break;
        }
        for(int j=i+1;j<alen;j++){
            if(sa[i]-sa[j]==0){
                isDf=false;
                break;
            }
        }
    }
    return isDf;
}

int main(int argc, char** argv) {
    long n;
    cin>>n;
    
    int nlen=IntLen(n);
    long a,b,c;        // n=a+c/b ; b是分子,c是分母 
    int cnt=0;
    for(long i=1;i<n;i++){
        a=i;
        int alen=IntLen(a);
        if( IsDiff(a) ){
        
            for(long j=1; IntLen(j)+IntLen( (n-a)*j )+alen<=9 ;j++){
                b=j;
                if( IsDiff(b) && IsOneOccur(a,b) ){
                
                    c=(n-a)*b;
                    if( IntLen(b)+IntLen( c )+alen==9  && IsDiff(c)  && IsOneOccur(a,c) && IsOneOccur(b,c)  ){
                        cnt++;
                        //cout<<a<<"+"<<c<<"/"<<b<<endl;
                    }
                }
            }
        }
    }    

    cout<<cnt<<endl;
        
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值