Brute force solution fraction split uva 10976

#include<cstdio>
using namespace std;
const int maxn = 1000;
int a[maxn],b[maxn];
int main() {
    int k, x, y;
    while(~scanf("%d", &k)) {
        int i = 0;
        for(y = k + 1; y <=2*k; y++)
            if( (k * y) % (y - k) == 0) {
                x = (k * y) / (y - k) ;
                a[i] = x;
                b[i] = y;
                i++;
            }
        printf("%d\n", i);
        for(int j = 0; j < i; j++) printf("1/%d = 1/%d + 1/%d\n", k, a[j], b[j]);
    }
    return 0;
}

1. How to judge whether x is an integer? - use directly

(k * y) % (y - k)

Instead of modulo 1 with x (which is so stupid), what can be written in fractional form is a rational number!

2. There is no need to distinguish between odd and even numbers in this question, because once y takes 2*k+1, it will definitely be larger than x

3. How to output i before outputting the equation? ——Since you want to do this, you must save it first. There is no need to save it in the form of a string, because the important thing is the data (to catch the main contradiction). In computer science, the location of the data is very important! So you can save it directly in an array.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325639202&siteId=291194637