问题:
输入正整数k,找到所有的正整数x>=y,使得1/k = 1/ x + 1/y.
样例输入:2
输出:1/2=1/6+1/3
1/2=1/4+1/4
样例输入:12
输出:1/12=1/156+1/13
1/12=1/84+1/14
1/12=1/60+1/15
1/12=1/48+1/16
1/12=1/36+1/18
1/12=1/30+1/20
1/12=1/28+1/21
1/12=1/24+1/24
解题关键:因为要求x>=y,所以y最大为x=y=2*k时,最小为k+1; 通过式子变形得到 x = y*k/(y-k);
解:
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for (int b = n + 1; b <= 2 * n; b++) {
if ((n * b) % (n - b) == 0) {
int a = (n * b) / (b - n);
System.out.printf("1/%s = 1/%s + 1/%s", n, a, b);
System.out.println();
}
}
}
}