形如:1/a 的分数称为单位分数。 可以把1分解为若干个互不相同的单位分数之和。

形如: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项时的所有不同分解法。

import java.util.Scanner;
 
  public class Danweifenshu {
      
      public static int n;
      public static long Lcd_1_30 = 2329089562800L; //1-30的最小公倍数
     public static int[] A;
      
      public void dfs(int step, int nowNum, long result) {
         if(step == n) {
             if(result != Lcd_1_30)
                 return;
             for(int i = 0;i < n;i++)
                 System.out.print("1/"+A[i]+" ");
             System.out.println();
             return;
         }
         if(result > Lcd_1_30)
             return;
         for(int i = nowNum + 1;i < 30;i++) {
             A[step] = i;
             dfs(step + 1, i, result + Lcd_1_30 / i);
         }
     }
     
	public static void main(String[] args) {
		Danweifenshu test = new Danweifenshu();
		Scanner in = new Scanner(System.in);
		n = in.nextInt();
		A = new int[n];
		test.dfs(0, 0, 0);
	}
 }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值