Mixed number (exhaustive method) Blue Bridge Cup title

Problem Description (Blue Bridge Cup practice systems may be submitted)

100 may be represented in the form of mixed fractions: 69 258 + 3 = 100/714.

It can also be expressed as: 82 + 100 = 3546/197.

Note wherein: a mixed fraction, the numbers 1 to 9 appears once and only once, respectively (not including 0).

Mixed fraction like this, there are 11 100 notation.

Input Format

From the standard input a positive integer N (N <1000 * 1000)

Output Format

With a digital program outputs the digital 1 ~ 9 will not be repeated without omission mixed fraction consisting of all the number of species represented.

Note: does not require the output of each said only statistics on the number representation!

Sample input 1
100
Sample output 1
11
Sample input 2
105
Sample output 2
6
 
I problem-solving ideas (among others): Enumeration method
Since the numbers are different, then an array can be initialized [1,2,3,4,5,6,7,8,9], then the full array are arranged. For each of the permutations and combinations of the foregoing, it was cut into three, a, b, c, and then to take the number of each combination of a, b, c of.
For example, when the array is full array [3,6,9,2,5,8,7,1,4], as a first value, if a = 3 (of course, can take a further 36,369 .. ., the premise is not greater than or equal n-a), then b, c of this value can be taken, starting after 3, any of them is inserted between the "/", "/" in front is B, C is the back, in a, b, c values ​​are obtained each time to verify a + b / c == n, that it is +1.
 
When a permutation is acquired (I used next_permutation () function to get too), available
When a = 3, b, c preferably 6 / 9258714,69 / 258714,692 / 58714 ...  
When a = 36, b, c preferably 9 / 258714,92 / 58714,925 / 8714 ...
When when a = 369, b, c preferably 2 / 58714,25 / 8714,258 / 714 .. (if n = 100, then the time is a value of an error)
......
 
Details: c can not be 0
 
 
#include <iostream>
#include <algorithm>

using namespace std;

// 拿数字 
int toNum(int *data,int s, int e) {
    int ret = 0;
    for (int i = s; i <= e; i++) {
        right = K * 10 + data [i];
    }
    Return the right;
}

int main() {
    int data[10];
    int n, ans,s,e,a,b,c,flag;
    while(~scanf("%d", &n)) {
        for (int i = 0; i < 10; i++) data[i] = i;
        ans = 0;
        do {
            s = e = 1;
            while(1) {
                a = toNum (Data, S, E); // determine a value obtained 
                IF (a> = n-) BREAK ; // when the acquired value is greater than a cross-sectional n, an error has been described too, are arranged directly next 
                 // moving from left to right so that a score-digit number 
                for ( int I = E + . 1 ; I < 10 - . 1 ; I ++) { // must ensure that there are a number c, otherwise the denominator is zero, so that i <10- . 1 
                    B = toNum (Data, + E . 1 , I);
                    c = toNum(data, i+1, 9);
                    if (b%c != 0) continue;
                    if (a+b/c==n)  ans++;
                }
                E ++; // move 
            }
        }while(next_permutation(data+1, data+10));    // 全排列 
        cout << ans << endl;
    }
    return 0;
}

 

Here there is the sound of silence.

 
 
 

Guess you like

Origin www.cnblogs.com/hello-dummy/p/12178757.html