Example 7-3 Fractionation (Fractions Again?!, UVa 10976)

Welcome to visit my Uva problem solution directory https://blog.csdn.net/richenyunqi/article/details/81149109

Title description

Example 7-3 Fractions Again?! (UVa 10976) title description

Interpretation

Enter a positive integer k and find all positive integers x ≥ yx≥yxy, 使得1 k = 1 x + 1 y \ frac {1} {k} = \ frac {1} {x} + \ frac {1} {y}k1=x1+Y1

algorithm design

Obviously, when x ≥ yx≥yxy , the range of y isy ∈ [k + 1, kY[k+1,k+k ] , just enumerate y, if1 k − 1 y \frac{1}{k}-\frac{1}{y}k1Y1The numerator of can be simplified to 1, that is, the numerator can divide the denominator, and a set of x and y that meet the conditions are found.

C++ code

#include <bits/stdc++.h>
using namespace std;
int main() {
    
    
    int k;
    while(cin>>k){
    
    
        vector<pair<int,int>>v;
        for(int i=k+1;i<=k+k;++i)
            if(i*k%(i-k)==0)//分母对分子的余数为0
                v.push_back({
    
    i*k/(i-k),i});//找到了一组满足条件的x与y
        printf("%d\n",v.size());
        for(auto&i:v)
            printf("1/%d = 1/%d + 1/%d\n",k,i.first,i.second);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/richenyunqi/article/details/100376042