历届试题 带分数 搜索

题目:

问题描述

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

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

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

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

输入格式

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

输出格式

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

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

样例输入1

扫描二维码关注公众号,回复: 5528655 查看本文章

100

样例输出1

11

样例输入2

105

样例输出2

6

思路:

将1-9的全排列列出来,然后进行判定,因为满足n=a+b/c,所以a<n,b>c,根据限制条件进行剪纸。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
ll n;
int vis[15];
int ans=0;
int w;
ll pow (int a,int b)
{
    ll ans=1;
    while(b--)
    {
        ans*=a;
    }
    return ans;
}
void Judge(ll x)
{
    ll t;
    ll a,b,c,q;
    for (int i=1;i<=w;i++)
    {
        t=x;
        a=t%pow(10,i);
        if(a>=n) continue;
        t/=pow(10,i);
        q=9-i;
        for (int j=1;j<=q/2;j++)
        {
            ll tt=t;
            b=tt%pow(10,j);
            tt/=pow(10,j);
            if(tt<b) continue;
            if(tt%b) continue;
            if(a+tt/b==n) {
                    ans++;
            }
        }
    }
}
void Sort (int m,ll x)
{
    if(m==9)
    {
        Judge(x);
    }
    for (int i=1;i<=9;i++)
    {
        if(vis[i]==0)
        {
            vis[i]=1;
            Sort(m+1,x*10+i);
            vis[i]=0;
        }
    }
}
int main()
{
    w=0;
    scanf("%lld",&n);
    int t=n;
    while(t)
    {
        t/=10;
        w++;
    }
    memset (vis,0,sizeof(vis));
    Sort(0,0);
    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/88424614