第五届第六题


形如:1/a 的分数称为单位分数。

可以把1分解为若干个互不相同的单位分数之和。
例如:
1 = 1/2 + 1/3 + 1/9 + 1/18
1 = 1/2 + 1/3 + 1/10 + 1/15
1 = 1/3 + 1/5 + 1/7 + 1/9 + 1/11 + 1/15 + 1/35 + 1/45 + 1/231
等等,类似这样的分解无穷无尽。

我们增加一个约束条件:最大的分母必须不超过30

请你求出分解为n项时的所有不同分解法。

数据格式要求:

输入一个整数n,表示要分解为n项(n<12)
输出分解后的单位分数项,中间用一个空格分开。
每种分解法占用一行,行间的顺序按照分母从小到大排序。

例如,
输入:
4
程序应该输出:
1/2 1/3 1/8 1/24
1/2 1/3 1/9 1/18
1/2 1/3 1/10 1/15
1/2 1/4 1/5 1/20
1/2 1/4 1/6 1/12

再例如,
输入:
5
程序应该输出:
1/2 1/3 1/12 1/21 1/28
1/2 1/4 1/6 1/21 1/28
1/2 1/4 1/7 1/14 1/28
1/2 1/4 1/8 1/12 1/24
1/2 1/4 1/9 1/12 1/18
1/2 1/4 1/10 1/12 1/15
1/2 1/5 1/6 1/12 1/20
1/3 1/4 1/5 1/6 1/20


资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗  < 2000ms


请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。

-------------------------------------

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if (a==1) {
                System.out.println("1/1");
}
if (a==3) {
    for (int b = 1; b < 30; b++) {
        for (int c = 2; c < 30; c++) {
            for (int d = 3; d < 30; d++) {
                    if (b*c+c*d+d*b==b*c*d&c>b&d>c) {
                        System.out.println("1/"+b+" "+"1/"+c+" "+"1/"+d);
                    }
                }
            }
        }
    }
if (a==4) {
    for (int b = 1; b < 30; b++) {
        for (int c = 2; c < 30; c++) {
            for (int d = 3; d < 30; d++) {
                for (int e = 4; e < 30; e++) {
                    if (b*c*d+c*d*e+d*e*b+e*b*c==b*c*d*e&c>b&d>c&e>d) {
                        System.out.println("1/"+b+" "+"1/"+c+" "+"1/"+d+" "+"1/"+e);
                    }
                }
            }
        }
    }
}
if (a==5) {
    for (int b = 1; b < 30; b++) {
        for (int c = 2; c < 30; c++) {
            for (int d = 3; d < 30; d++) {
                for (int e = 4; e < 30; e++) {
                    for (int f = 5; f < 30; f++) {
                    if (b*c*d*e+f*b*c*d+f*c*d*e+d*f*e*b+f*e*b*c==f*b*c*d*e&f>e&c>b&d>c&e>d) {
                        System.out.println("1/"+b+" "+"1/"+c+" "+"1/"+d+" "+"1/"+e+" "+"1/"+f);
                    }    
                    }
                }
            }
        }
    }
}
if (a==6) {
    for (int b = 1; b < 30; b++) {
        for (int c = 2; c < 30; c++) {
            for (int d = 3; d < 30; d++) {
                for (int e = 4; e < 30; e++) {
                    for (int f = 5; f < 30; f++) {
                        for (int g = 6; g < 30; g++) {
                        if (g*b*c*d*e+g*f*b*c*d+g*f*c*d*e+g*d*f*e*b+g*f*e*b*c+f*b*c*d*e==g*f*b*c*d*e&g>f&f>e&c>b&d>c&e>d) {
                            System.out.println("1/"+b+" "+"1/"+c+" "+"1/"+d+" "+"1/"+e+" "+"1/"+f+" "+"1/"+g);
                            }
                        }    
                    }
                }
            }
        }
    }
}
if (a==7) {
    for (int b = 1; b < 4; b++) {
        for (int c = 4; c < 9; c++) {
            for (int d = 5; d < 30; d++) {
                for (int e = 6; e < 30; e++) {
                    for (int f = 7; f < 30; f++) {
                        for (int g = 12; g < 30; g++) {
                            for (int h = 15; h < 30; h++) {
                            if (h*g*b*c*d*e+h*g*f*b*c*d+h*g*f*c*d*e+h*g*d*f*e*b+h*g*f*e*b*c+h*f*b*c*d*e+g*f*b*c*d*e==h*g*f*b*c*d*e&h>g&g>f&f>e&c>b&d>c&e>d) {
                                System.out.println("1/"+b+" "+"1/"+c+" "+"1/"+d+" "+"1/"+e+" "+"1/"+f+" "+"1/"+g+" "+"1/"+h);
                                }
                            }
                        }    
                    }
                }
            }
        }
    }
}
if (a==8) {
    for (int b = 2; b < 5; b++) {
        for (int c = 5; c < 9; c++) {
            for (int d = 6; d < 11; d++) {
                for (int e = 8; e < 13; e++) {
                    for (int f = 10; f < 21; f++) {
                        for (int g = 14; g < 22; g++) {
                            for (int h = 15; h < 25; h++) {
                                for (int j = 20; j < 30; j++) {
                                if (j*h*g*b*c*d*e+j*h*g*f*b*c*d+j*h*g*f*c*d*e+j*h*g*d*f*e*b+j*h*g*f*e*b*c+j*h*f*b*c*d*e+j*g*f*b*c*d*e+h*g*f*b*c*d*e==j*h*g*f*b*c*d*e&j>h&h>g&g>f&f>e&c>b&d>c&e>d) {
                                    System.out.println("1/"+b+" "+"1/"+c+" "+"1/"+d+" "+"1/"+e+" "+"1/"+f+" "+"1/"+g+" "+"1/"+h+" "+"1/"+j);
                                    }
                                }
                            }
                        }    
                    }
                }
            }
        }
    }
}
if (a==9) {
    for (int b = 2; b < 5; b++) {
        for (int c = 4; c < 10; c++) {
            for (int d = 5; d < 11; d++) {
                for (int e = 6; e < 13; e++) {
                    for (int f = 8; f < 21; f++) {
                        for (int g = 13; g < 22; g++) {
                            for (int h = 14; h < 25; h++) {
                                for (int j = 18; j < 25; j++) {
                                    for (int k = 20; k < 30; k++) {
                                    if (k*j*h*g*b*c*d*e+k*j*h*g*f*b*c*d+k*j*h*g*f*c*d*e+k*j*h*g*d*f*e*b+k*j*h*g*f*e*b*c+k*j*h*f*b*c*d*e+k*j*g*f*b*c*d*e+k*h*g*f*b*c*d*e+j*h*g*f*b*c*d*e==k*j*h*g*f*b*c*d*e&k>j&j>h&h>g&g>f&f>e&c>b&d>c&e>d) {
                                        System.out.println("1/"+b+" "+"1/"+c+" "+"1/"+d+" "+"1/"+e+" "+"1/"+f+" "+"1/"+g+" "+"1/"+h+" "+"1/"+j+" "+"1/"+k);
                                        }
                                    }
                                }
                            }
                        }    
                    }
                }
            }
        }
    }
}
if (a==10) {
    for (int b = 2; b < 6; b++) {
        for (int c = 4; c < 10; c++) {
            for (int d = 6; d < 10; d++) {
                for (int e = 7; e < 11; e++) {
                    for (int f = 8; f < 13; f++) {
                        for (int g = 12; g < 19; g++) {
                            for (int h = 14; h < 21; h++) {
                                for (int j = 17; j < 22; j++) {
                                    for (int k = 19; k < 25; k++) {
                                        for (int l = 1; l < 29; l++) {
                                        if (l*k*j*h*g*b*c*d*e+l*k*j*h*g*f*b*c*d+l*k*j*h*g*f*c*d*e+l*k*j*h*g*d*f*e*b+l*k*j*h*g*f*e*b*c+l*k*j*h*f*b*c*d*e+l*k*j*g*f*b*c*d*e+l*k*h*g*f*b*c*d*e+l*j*h*g*f*b*c*d*e+k*j*h*g*f*b*c*d*e==l*k*j*h*g*f*b*c*d*e&l>k&k>j&j>h&h>g&g>f&f>e&c>b&d>c&e>d) {
                                            System.out.println("1/"+b+" "+"1/"+c+" "+"1/"+d+" "+"1/"+e+" "+"1/"+f+" "+"1/"+g+" "+"1/"+h+" "+"1/"+j+" "+"1/"+k+" "+"1/"+l);
                                            }
                                        }
                                    }
                                }
                            }
                        }    
                    }
                }
            }
        }
    }
}
if (a==11) {
    for (int b = 4; b < 7; b++) {
        for (int c = 5; c < 8; c++) {
            for (int d = 8; d < 10; d++) {
                for (int e = 9; e < 11; e++) {
                    for (int f = 10; f < 12; f++) {
                        for (int g = 12; g < 16; g++) {
                            for (int h = 14; h < 19; h++) {
                                for (int j =14; j < 21; j++) {
                                    for (int k = 18; k < 22; k++) {
                                        for (int l = 24; l < 25; l++) {
                                            for (int z = 28; z < 30; z++) {
                                            if (z*l*k*j*h*g*b*c*d*e+z*l*k*j*h*g*f*b*c*d+z*l*k*j*h*g*f*c*d*e+z*l*k*j*h*g*d*f*e*b+z*l*k*j*h*g*f*e*b*c+z*l*k*j*h*f*b*c*d*e+z*l*k*j*g*f*b*c*d*e+z*l*k*h*g*f*b*c*d*e+z*l*j*h*g*f*b*c*d*e+z*k*j*h*g*f*b*c*d*e+l*k*j*h*g*f*b*c*d*e==z*l*k*j*h*g*f*b*c*d*e&z>l&l>k&k>j&j>h&h>g&g>f&f>e&c>b&d>c&e>d) {
                                                System.out.println("1/"+b+" "+"1/"+c+" "+"1/"+d+" "+"1/"+e+" "+"1/"+f+" "+"1/"+g+" "+"1/"+h+" "+"1/"+j+" "+"1/"+k+" "+"1/"+l+" "+"1/"+z);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }    
                    }
                }
            }
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值