蓝桥杯——带分数

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

1.首先运用组合的形式把所有情况输出来

2.然后,按要求找到所有情况。

注意,将输入的N强制转换成double类型,这样求才能把得到准确的结果。

 

#include <iostream>
#include <string.h>
using namespace std;

int sum = 0;
void swap(char *a,int m,int n,int h)
{
    if(m == 0)
    {
        double one = a[0]-'0';
        double two = (a[1]-'0')*10000+(a[2]-'0')*1000+(a[3]-'0')*100+(a[4]-'0')*10+(a[5]-'0');
        double three = (a[6]-'0')*100+(a[7]-'0')*10+(a[8]-'0');
        
        double one1 = (a[0]-'0')*10+(a[1]-'0');
        double two2 = (a[2]-'0')*1000+(a[3]-'0')*100+(a[4]-'0')*10+(a[5]-'0');
        double three3 = (a[6]-'0')*100+(a[7]-'0')*10+(a[8]-'0');
        if(((double)h == (one+two/three))||((double)h == (one1+two2/three3)))
        {
            sum++;
            //cout<<one<<two<<three<<endl;/*输出每一种情况*/
        }
        
        
        return ;
    }
    else{
        char t;
        for(int j=0;j<=m;j++)
        {
            t = a[j];
            a[j] = a[m];
            a[m] = t;
            swap(a,m-1,n,h);
            t = a[j];
            a[j] = a[m];
            a[m] = t;
        }
    }
}

int main()
{
    int n;
    cin>>n;
    char a[10] = {'1','2','3','4','5','6','7','8','9'};
    swap(a,8,9,n);
    cout<<sum<<endl;
    return 0;
}

 

结果

输入

100

输出

941578263
369258714
917524836
817524396
962148537
915823647
961428357
823546197
815643297
961752438
915742638


11

 

import java.util.Scanner;


public class Main
{
    static int art [] = {1,2,3,4,5,6,7,8,9};
    static int nn;
    static int count = 0;
    
    public static void print()
    {
        for(int i:art)
        {
            System.out.print(i);
        }
        System.out.println();
    }
    
    public static boolean manzu()//判断该结果是否满足
    {
        int a = art[0]*10+art[1];
        int b = art[2]*1000+art[3]*100+art[4]*10+art[5];
        int c = art[6]*100+art[7]*10+art[8];
        
        int a1 = art[0];
        int b1 = art[1]*10000+art[2]*1000+art[3]*100+art[4]*10+art[5];
        int c1 = art[6]*100+art[7]*10+art[8];
        
        if(nn*c == a*c+b || nn*c1== a1*c1+b1)//满足条件
        {
            return true;
        }
        return false;
        
    }
    
    public static void fun(int n)//输出每一种结果
    {
        if(n == 0)
        {
            if(manzu())
            {
                count++;
                print();
            }
            return;
        }
        
        for(int i=0;i<=n;i++)
        {
            int temp;
            temp = art[i];
            art[i] = art[n];
            art[n] = temp;
            fun(n-1);
            temp = art[i];
            art[i] = art[n];
            art[n] = temp;
        }
        
    }
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        Scanner scanner = new Scanner(System.in);
        nn = scanner.nextInt();
        fun(8);
        System.out.println(count);

    }

}

 

posted @ 2019-03-03 12:52  池塘之底  阅读(242)  评论(0编辑  收藏  举报