求解的关键是对等式变形,1/x <= 1/y ,1/k = 1/x + 1/y <= 1/y + 1/y = 2/y ,所以y >= 2k;再根据x=(k*y) / (y - k);求解x,判断x是否为整数即可。根据x的表达式可知:y>k
判断一个数是否为整数,用floor(x+0.5) == x
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
struct Index{
int x;
int y;
};
int main()
{
int n;
while(cin >> n){
vector<Index> result;
for(int y=n ; y<=2*n ; y++){
int x = floor(1.0*n*y/(y-n))+0.5;
if(1.0*n*y/(y-n) == x){
// cout << "1/" << n << "=1/"<<x << "+1/" << y << endl;
Index t = {x,y};
result.push_back(t);
}
}
cout << result.size() << endl;
for(int i=0 ; i<result.size() ; i++){
cout << "1/" << n << "=1/"<<result[i].x << "+1/" << result[i].y << endl;
}
}
return 0;
}