java中的递归,任何人都可以帮助我吗? [英] Recursion in java , can anyone please help me?

查看:74
本文介绍了java中的递归,任何人都可以帮助我吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定是在(1)下定义的序列,递归地描述如下:

pi = 1(对于0≤i≤2)

pi = a对于i,* p(i-2)+ b * p(i-3)> 2

通过改变a和b的值,可以计算出许多其他序列。立即写入

Java程序,用i≤n和n∈N∪{0}计算所有以下元素pi。程序启动后立即

您的程序将接受三个命令行参数。数字n是要交出的第一个命令行参数

。参数a作为第二个命令行参数传递。作为第三个

命令行参数传递给程序。它认为a,b∈Z。

请注意,必须存在所有命令行参数。在处理

命令行参数时发生错误或输入与此处指定的格式不对应,因此

输出以Error开头的有意义的错误消息,并且程序终止。

通过使用System.out.println(),计算出的序列元素pi直到索引号n,用一行逗号分隔一个逗号

输出。

(1)定义的计算规则的原始递归实现将一遍又一遍地重新计算相同的跟随元素。通过

使用所谓的查找表,应该避免这种情况。这意味着,一旦添加了先前未知的
Pi,pi的值就会添加到查找表中。因此,这个过程旨在防止代价高昂的计算和优化运行时。选择

a适合执行查找表的数据结构



我尝试过:



Given is the sequence defined under (1), which is described recursively as follows:
pi =1( for 0 ≤ i ≤ 2)
pi = a * p(i-2) + b * p(i-3) for i> 2
By varying the values ​​of a and b, numerous other sequences can be calculated. Write now
Java program that computes all the following elements pi with i ≤ n and n ∈ N ∪ {0}. Right after the program starts
Your program will accept three command line arguments. The number n is the first command line argument
to hand over. The parameter a is passed as a second command line argument. As third
Command line argument is passed to the program. It holds that a, b ∈ Z.
Please note that all command line arguments must be present. Occurs when processing the
Command line arguments an error or the input does not correspond to the format specified here, so
A meaningful error message starting with Error, is output and the program is terminated.
The calculated sequence elements pi up to the index number n are separated in a line by exactly one comma
output by using System.out.println (). A primitive recursive implementation of the
(1) defined calculation rule would recalculate the same follower elements over and over again. By the
Using a so-called lookup table this should be avoided. That means, once a previously unknown
Pi is added, the value of pi is added to the lookup table. Because of this
This procedure is intended to prevent costly calculations and to optimize the runtime. Choose
a suitable data structure for the implementation of the lookup table

What I have tried:

public class Main {

    public static void main(String[] args) {
        System.out.println(pi(4));

    }

    public static String pi(int n) {
         
        String result = "";
        int a = 1;
        int b = 1;
        for (int i = 0; i <= n; i++) {
            
            if (i >= 0 && i <= 2) {
                result = 1 + "";

            } else {
                result = a*pi(i - 2) + b*pi(i - 3) + "";
            }
            if (i < n - 1) {
                result += ",";

            }

        }
        return result;
    }
}

推荐答案

我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!

再次阅读这个问题,并将其与你迄今为止的产品进行比较......它还没有关闭,是吗?



如果您遇到特定问题,请询问相关问题,我们会尽力提供帮助。但我们不打算为你做这一切!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!
Read the question again, and compare that to what you have produced so far... it's not even close yet, is it?

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


仔细阅读要求:

Read carefully the requirements:
Quote:

您的程序将接受三个命令行参数。数字n是要交出的第一个命令行参数

。参数a作为第二个命令行参数传递。作为第三个

命令行参数传递给程序。它认为a,b∈Z。

Your program will accept three command line arguments. The number n is the first command line argument
to hand over. The parameter a is passed as a second command line argument. As third
Command line argument is passed to the program. It holds that a, b ∈ Z.



你的程序没有。


Your program don't.

Quote:

请注意,所有命令行参数必须存在。在处理

命令行参数时发生错误或输入与此处指定的格式不对应,因此

输出以Error开头的有意义的错误消息,并且程序终止。

Please note that all command line arguments must be present. Occurs when processing the
Command line arguments an error or the input does not correspond to the format specified here, so
A meaningful error message starting with Error, is output and the program is terminated.



你的程序没有。

你对pi()的计算很乐意混合其他要求,使它完全错误。

建议:

制作一段代码来计算pi(x)而不是其他任何东西。然后你可以将它用于你想要的任何东西。

制作程序而不是从1到N的打印值基本上与从pi(1)到pi(N)打印值的程序相同。 />
保持简单,确保程序有效,然后改进。



我们不做你的HomeWork。

HomeWork问题是你在现实生活中必须解决的问题的简化版本,他们的目的是学习和练习


public class Main {

    public static void main(String[] args) {
        Terminal.printLine("Please enter n , a and b ");
        int n = Integer.parseInt(args[0]);
        int a = Integer.parseInt(args[1]);
        int b = Integer.parseInt(args[2]);
        for (int i = 1; i <= n; i++)
            Terminal.printLine(i + "," + recursiv(n));

    }

    public static long recursiv(int n) {

        if (n >= 0 && n <= 2)
            return 1;

        else
            return a*recursiv(n - 2) + b*recursiv(n - 3);

    }

}


这篇关于java中的递归,任何人都可以帮助我吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆