2013 蓝桥杯C/C++本科B组 带分数(牛逼库函数)(备战LQB)

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


排列,牛逼函数:next_permutation()

使用举例:

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int a[4]= {1,2,3,4};
    while(next_permutation(a,a+4))
    {
        cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<endl;
    }
}

代码:


#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;

int f(int star,int end,int a[])
{
    if(star>end)
        return 1;
    int n=0;
    for(int i=star; i<=end; i++)
    {
        n=n*10+a[i];
    }
    return n;
}
int main()
{
    int n;
    int count=0;
    cin>>n;
    int size=0;
    int x=n;
    while(n)
    {
        size++;
        n=n/10;
    }
    int a[9]= {1,2,3,4,5,6,7,8,9};
    while(next_permutation(a,a+9)) //牛逼的全排函数。
    {
        for(int i=0; i<=size; i++)
        {
            for(int j=8-(8-i)/2; j<8; j++)
            {
                int x1=f(0,i,a);
                int x2=f(i+1,j,a);
                int x3=f(j+1,8,a);
                if(x==x1+x2/x3&&x2==x3*(x2/x3))
                {
                    count++;
                    cout<<x1<<"+"<<x2<<"/"<<x3<<endl;
                }
            }
        }
    }
    printf("%d\n",count);
    return 0;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值