Java的prefixAverages /阵列的帮助 [英] Java prefixAverages/array help

查看:329
本文介绍了Java的prefixAverages /阵列的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图转换两个不同的算法转换成Java code,以测试他们。第一种算法(prefixAverages1)是:

I am attempting to translate two separate algorithms into Java code in order to test them; the first algorithm (PrefixAverages1) is:

For i = 0 to n-1
    Let s = X[0]
    For j = 1 to i
        Let s = s + X[j]
    End For
    Let A[i] = s / (i + 1)
End For

输出:数字的n元素数组A使得A [i]为元素的平均X [0]中,X [1],......,X [I]

Output: An n-element array A of numbers such that A[i] is the average of elements X[0], X[1], ..., X[i].

这是我想出了prefixAverages1了Java编译:

This is the Java translation that I came up with for PrefixAverages1:

import java.util.Arrays;

public class PrefixAverages1 {

static double array[] = new double[10];

public static void prefixAverages(){

    for (int i = 0; i < 10; i++){

        double s = array[i];

            for (int j = 0; j < 10; j++){

                s = s + array[j];
            }

        array[i] = s / (i + 1);

        System.out.println(Arrays.toString(array));
    }

}

public static void main(String[] args){
    prefixAverages();
}

}

第二种算法(prefixAverages2)是:

The second algorithm (PrefixAverages2) is:

Let s = X[0]
For i = 0 to n-1
    Let s = s + X[i]
    Let A[i] = s / (i + 1)
End For

输出:数字的n元素数组A使得A [i]为元素的平均X [0]中,X [1],......,X [I]

Output: An n-element array A of numbers such that A[i] is the average of elements X[0], X[1], ..., X[i].

这是我想出了prefixAverages2了Java编译:

This is the Java translation that I came up with for PrefixAverages2:

import java.util.Arrays;

public class PrefixAverages2 {

static double array[] = new double[10];

public static void prefixAverages(){

        double s = 0;

        for (int i = 0; i < 10; i++){
                s = s + array[i];
                array[i] = s / (i + 1);
        }
            array[0] = 10;

        System.out.println(Arrays.toString(array));
}

public static void main(String[] args){
    prefixAverages();
}

}

我试图测试的算法,但我不太知道从哪里开始。我试图建立一个数组,然后通过它们运行手动对n的值较小申请就可以了这些算法。

I am attempting to test the algorithms but am not quite sure where to start. I am attempting to set up an array and then apply these algorithms on it by running through them by hand for small values of n.

在哪里/我将如何开始的元素添加到阵列开始呢?

Where/how would I begin to add elements to the array to begin with?

我也试图通过计算基本操作来分析算法和派生T(N)两种算法。我希望能够制定出每种算法的时间复杂度(大哦,O(N)),并发现哪一个是最高效的。

I am also trying to analyse both algorithms by counting primitive operations and derive T(n) for both algorithms. I would like to be able to work out the time complexity (Big Oh, O(n)) of each algorithm and discover which one is the most efficient.

任何帮助,这将大大AP preciated。

Any help with this would be greatly appreciated.

感谢您抽出时间来阅读和协助的时间。

Thanks for taking the time to read and assist.

推荐答案

首先,你应该通过阵列的功能,并返回结果。

First of all, you should pass in the array to the function and return the result.

public static double[] prefixAverages(double[] inArray) {
    double[] outArray = new double[inArray.length];
    //...compute the averages...
    return outArray;
}

然后,你可以做一些手工测试中,你的主要方法:

Then you can do some manual testing in you main method:

public static void main(String... args) {
    System.out.println(
       Arrays.equals(
          prefixAverages(new double[] {1, 2, 3, 4}),
          new double[] {1, 1.5, 2, 2.5}
       )
    );
}

确认此code打印

现在,这不是一个非常强大的,因为与浮点数精度限制的解决方案,但它应该让你开始。如果你想真正看看测试你的code,您应该研究使用测试工具如的JUnit

Now, that's not a very robust solution because of accuracy limitations with floating-point numbers, but it should get you started. If you want to really look into testing your code, you should research using a test harness like JUnit.

这篇关于Java的prefixAverages /阵列的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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