为了账号安全,请及时绑定邮箱和手机立即绑定

查找并打印 x1+x2+x3=num 的解数

查找并打印 x1+x2+x3=num 的解数

慕的地8271018 2022-12-28 15:41:44
我需要编写一个recusive函数,它接收一个整数num并返回方程的解数:x1 + x2 + x3 = num,其中x1,x2,x31-10 之间的数字,该方法应打印所有解。例如,如果num=3那么该方法将打印1+1+1并返回1。如果num=5该方法将返回6并打印:1 + 1 + 31 + 2 + 21 + 3 + 12 + 1 + 22 + 2 + 13 + 1 + 1如果num<3或num>30方法将返回0.该方法应该是递归的而不使用循环。不允许使用全局变量。列表也是不允许的。这是我的代码,它工作正常但它也打印重复项,因为num=5它打印:3 + 1 + 12 + 2 + 12 + 1 + 22 + 2 + 11 + 3 + 11 + 2 + 22 + 1 + 21 + 2 + 21 + 1 + 3这是我的代码:public static void main(String[] args) {    System.out.println("num of solutions: "+solutions(5));}public static int solutions(int num) {    if (num < 3 || num > 30)        return 0;    return solutions(num, 1, 1, 1);}private static int solutions(int num, int x1, int x2, int x3){    if (x1 < 1 || x1 > 10 || x2 < 1 || x2 > 10||x3 < 1 || x3 > 10)        return 0;    if (x1 + x2 + x3 > num)        return 0;           if (x1 + x2 + x3 == num)    {        System.out.println(x1 + " + " + x2 + " + " + x3);        return 1;    }               return solutions(num, x1 + 1, x2, x3) + solutions(num, x1, x2 + 1, x3) + solutions(num, x1, x2, x3 + 1);}如何在没有重复的情况下获得所需的输出?
查看完整描述

4 回答

?
神不在的星期二

TA贡献1963条经验 获得超6个赞

你得到重复的原因是两者solutions(1,2,1)都会solutions(2,1,1)引导你到2 + 2 + 1.


不重复三位数的简单方法是从 111 递增到 10,10,10,就好像它是一个十进制整数一样:


private static int solutions(int num, int x1, int x2, int x3)

{

  if (x1 > 10 || x1 > num)

    return 0;

  if (x2 > 10 || x1+x2 > num)

    return solutions(num, x1+1, 1, 1);

  if (x3 > 10 || x1+x2+x3 > num)

    return solutions(num, x1, x2+1, 1);


  int me = 0;

  if (x1+x2+x3 == num) {

    System.out.printf("%d + %d + %d\n", x1, x2, x3);

    me=1;

  }

  return me + solutions(num, x1, x2, x3+1);

}

这模仿了您通过修剪搜索整个空间的方法,但更有效的解决方案可以只搜索x1和x2并设置x3=num-x1-x2。


查看完整回答
反对 回复 2022-12-28
?
白板的微信

TA贡献1883条经验 获得超3个赞

我们可以使用字符串来解决这个问题。声明一个全局字符串变量


static String str=""; // taken null intially

现在,我们可以使用这个字符串 str 来存储序列并检查它是否已经出现在前面。这样我们就可以跟踪重复的问题,您将得到答案。我已附上我的代码如下。


private static int solutions(int num, int x1, int x2, int x3)

{

    if (x1 < 1 || x1 > 10 || x2 < 1 || x2 > 10||x3 < 1 || x3 > 10)

        return 0;

    if (x1 + x2 + x3 > num)

        return 0;       

    if (x1 + x2 + x3 == num)

    {

        String s= String.valueOf(x1)+"+"+String.valueOf(x2)+"+"+String.valueOf(x2);

        if(!str.contains(s))

        {

            str=str+s+"\n";

            System.out.println(x1 + " + " + x2 + " + " + x3);

            return 1;

        }

    }           

    return solutions(num, x1 + 1, x2, x3) + solutions(num, x1, x2 + 1, x3) + solutions(num, x1, x2, x3 + 1);


}


查看完整回答
反对 回复 2022-12-28
?
胡子哥哥

TA贡献1825条经验 获得超6个赞

好吧......没有集合,没有全局变量,没有重复项。我希望你被允许使用 StringBuilder?


public static void main(String[] args) {

    StringBuilder sb = new StringBuilder();


    System.out.println("num of solutions: " + solutions(5, sb));


    System.out.println(sb.toString());

}


public static int solutions(int num, StringBuilder sb) {

    if (num < 3 || num > 30)

        return 0;


    return solutions(num, 1, 1, 1, sb);

}


private static int solutions(int num, int x1, int x2, int x3, StringBuilder sb) {

    if (x1 > 10 || x2 > 10 || x3 > 10) {

        return 0;

    }


    if (x1 + x2 + x3 > num) {

        return 0;

    }


    if (x1 + x2 + x3 == num) {

        String str = x1 + " + " + x2 + " + " + x3;

        if (!sb.toString().contains(str)) {

            sb.append(str).append(System.lineSeparator());

            return 1;

        }

    }


    return solutions(num, x1 + 1, x2, x3, sb) 

           + solutions(num, x1, x2 + 1, x3, sb) 

           + solutions(num, x1, x2, x3 + 1, sb);

}

结果:


    num of solutions: 6

    3 + 1 + 1

    2 + 2 + 1

    2 + 1 + 2

    1 + 3 + 1

    1 + 2 + 2

    1 + 1 + 3


查看完整回答
反对 回复 2022-12-28
?
森栏

TA贡献1810条经验 获得超5个赞

试试这个 :


public static void main(String... args) {

    System.out.println(solutions(5));

}

public static int solutions(int n) {


    if (n < 3 || n > 30) return 0;

    return solutions(n, n-2, 1, 1, 0);


}


public static int solutions(int n, int x1, int x2, int x3, int solutions) {


    ++solutions;

    System.out.println("Solution found : "+x1 +"+" + x2 + "+" + x3);


    if(x3 == n-2) return solutions;


    if(x2 > 1) {

        return solutions(n, x1, x2-1, x3+1, solutions);

    }


    if(x1 > 1) {

        return solutions(n, x1-1, n-x1, 1, solutions);

    }

    return solutions;

}

输出:6


这个想法如下:


您从尽可能大的 x1 开始。


然后你遵循这两个规则:


如果 x2 > 1 则 x2 = x2 - 1 且 x3 = x3 + 1


如果不是,并且如果 x1 > 1 THEN x1 = x1 - 1,x3 = 1 和 x2 = 获得正确总数所需的数量。


如果这两个条件都不成立,则没有更多的解决方案。


结果 :


3 + 1 + 1


第一个条件为假,第二个条件为真:我们将 1 移至 x1,x3 变为 1,x2 变为逻辑上的 2


2 + 2 + 1


第一个条件为真。我们从 x2 中删除 1,然后将 1 添加到 x3


2 + 1 + 2


第一个条件为假


第二个条件为真


1 + 3 + 1


第一个条件为真


1 + 2 + 2


第一个条件为真


1 + 1 + 3


第一个条件为假,第二个为假。


我们有 6 个解决方案,就是这样。


希望有所帮助!


查看完整回答
反对 回复 2022-12-28
  • 4 回答
  • 0 关注
  • 203 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号