java中的迭代求和

时间:2014-11-13 23:52:19

标签: java arrays algorithm sum

我在使用Java开发此算法时遇到麻烦,请按照以下步骤进行操作:

我有一组双打和一个变量double。

我需要开发一种算法,该算法将数组的值与变量相加 - 这部分是可以的。

问题是:我需要在x次上迭代这个总和(例如2次,3次,......或80次),并且变量sum的值与数组的所有值一起,并且“将其值传播到数组中的值,例如:

1.0 - double variable
0.1 - 1st value in array
0.5 - 2nd value in array
0.8 - 3rd value in array

在第一次迭代中,我们有 - 不要担心0.333! ;)

formule = 0.1 + (1 * 0.3333) = 0.433
formule = 0.5 + (1 * 0.333) = 0.8333
formule = 0.8 + (1 * 0.333) = 1.1333

所以,我们必须将所有结果与double变量相加,然后

0.433 + 0.833 + 1.1333 + 1 = 2.4

,在下一次迭代中,2.4值和所有其他值将在公式

中被替换
formule = 0.433 + (2.4 * 0.3333) = 1.2325
formule = 0.833 + (2.4 * 0.333) = 1.63325
formule = 1.133 + (2.4 * 0.333) = 1.932

所以,我们必须将所有结果与新的双变量(2.4)相加,然后

1.2325 + 1.63325 + 1.932 + 2.4 = 4.799

并且,该过程重复直到迭代完成!

该算法基于Melnik提出的相似性泛滥。

按照我的算法

 double [] sfAttributes = new double [3]; 

    double [] sfNewAttributes = new double [3]; 

    double [] simLinksAttributes = {0.1, 0.5, 0.8}; // array with values

    double sfClass = 0.0d;

    double simLinkClass = 1.0d; // the double variable!

    int iter = 3; // amout of iterations

    // set the first iteration value
    for (int i = 0; i < similaridadeLinksAtributos.length; i++){


        sfAttributes[i] = simLinkAttributes[i] + (simLinkClass * 0.333);

        // here, sum all values of the simLinkAttributes[i]
        // and put the results in sfClass variable
        sfClass +=  similaridadeLinksAtributos[i];

    }

    sfClasses = (sfClasses + simLinkClass);

 // here starts the iterations.

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

        for (int j = 0; j < simLinkAttributes.length; j++){


            sfNewAttributes[j] = sfAttributes[j] + (sfClass * 0.333);

            sfClass +=  sfNewAttributes[j];


    }

}

我需要达到的结果如下:

/* Results
         * 
         *       0 Interaction
         * 2,4 - simLinkClass variable
         * 0,433 - 1st new value of array
         * 0,833 - 2nd new value of array
         * 1,133 - 3rd new value of array
         * 
         *      1st Interaction
         * 4,799 - simLinkClass variable
         * 1,2325 - 1st new value of array
         * 1,63325 - 2nd new value of array
         * 1,932 - 3rd new value of array
         * 
         *      2nd Iteraction
         * 9,58675 - simLinkClass variable
         * 2,830567 - 1st new value of array
         * 2,3227562 -  2nd new value of array
         * 3,530067 -  3rd new value of array
         * 
         *      
         * 
         */

1 个答案:

答案 0 :(得分:0)

试试这个

    double[] sfAttributes = new double[3];
    double[] simLinksAttributes = {0.1, 0.5, 0.8}; // array with values
    double sfClass = 0.0d;
    double simLinkClass = 1.0d; // the double variable!
    int iter = 3; // amout of iterations

    // set the first iteration value
    for (int i = 0; i < simLinksAttributes.length; i++) {
        sfAttributes[i] = simLinksAttributes[i] + (simLinkClass * 0.333);
        // here, sum all values of the simLinkAttributes[i]
        // and put the results in sfClass variable
        sfClass += simLinksAttributes[i];
    }
    double[] sfNewAttributes = sfAttributes;

    sfClass = (sfClass + simLinkClass);
    System.out.println(sfClass);
    // here starts the iterations.

    for (int i = 0; i < iter; i++) {
        for (int j = 0; j < simLinksAttributes.length; j++) {
            sfNewAttributes[j] = sfNewAttributes[j] + (sfClass * 0.333);
        }

        for (int k =0 ; k < sfNewAttributes.length; k++) {
            sfClass += sfNewAttributes[k];
        }
        System.out.println(sfClass);
    }