通过数组去开始与不同的位置 [英] Go through an array starting with different positions

查看:140
本文介绍了通过数组去开始与不同的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图去通过一个数组:

 公共静态字符串计算(INT []账单,金额INT){
INT大小= bills.length;
INT缓存= 0;
的for(int i = 0; I<大小;我++){
    缓存+ =票据[我]
    如果(缓存==量){
        返回确定;
    }
}
返回不OK;
}

这将通过按此顺序排列(和和):

1 2 5 7 = 1,3,8,15(资金)

不过,我想是这样的:

第1次迭代:1 2 5 7 = 1,3,8,15

第二迭代:2 5 7 1 = 2,7,14,15

3迭代:5 7 1 2 = 5,12,13,15

第四迭代:7 1 2 5 = 7,8,10,15

请注意:我打电话第​​一,第二,第三,第四,因为我指的是2路的情况;
和技术上的什么我想要做的是,从第二循环等等,当它到达指数[3]它去,然后索引[0],并添加他们。然后从索引[2]开始并得到索引时[3]它概括指数[0]和[1]。

我试图在使用双不同的方法(和j = 1)和时(第2的)的第j
达到阵列的上次位置([3]在这种情况下)将其更改到零,大小= I(这将是该点它开始),但它没有正常工作...
也或许它可以用(%模量)工作,但我想看看别人的想法,它总是有助于学习:)谢谢你们!

PS:我用这一个作为数组(忽略量方法部分,因为不是问题的一部分):

  INT [] =票据INT新[] {1,2,5,7};

PSS:这是code我想(与双循环,这是不是一个与我做,因为我错过了它,因为我删除,改变了很多尝试新事物,这其中可能有失误少可怕的错误。):

 公共静态字符串计算(INT []账单,金额INT){    INT大小= bills.length;
    INT缓存= 0;
    INT C = 1;
    INT Z =大小-1;
    INT S = 0;
    INT W = 0;
    布尔TIME = FALSE;        的for(int i =瓦; I<大小;){            对于(INT J =; J<大小; J ++){
                缓存+ =账单[J]。
                如果(缓存==量){
                    缓存= 0;
                    返回确定;
                }
                的System.out.println(TIME);
                如果(ⅰ大于0&放大器;&放大器;Ĵ==ž&放大器;&放大器; TIME){
                    大小=我;
                    I = 0;
                    TIME = FALSE;
                }
            }
            W¯¯++;
            大小= Z + 1;
            缓存= 0;
            TIME =真实的;
        }
}


解决方案

下面是这是否一个简单的逻辑。您可以添加一个新的数组,然后你遍历帐单阵列,可以将索引加上previous之一的总和添加到新阵列,打印数组,然后重新排序票据阵列的数量和重复循环。

您可以使用 System.arrayCopy 改变你的账单数组元素的顺序。让我们假设你有

 账单> [2,5,7,1]:总和> [2,7,14,15]

现在,你需要旋转你的帐单数组,因此后续的这样做使用arrayCopy

  INT firstElement =账单[0];
System.arraycopy(票据,1,票据,0,大小-1);
票据[大小1] = firstElement;

以上三条线路,复制数组的第一个元素,那么使用arrayCopy复制账单[1] 票据[2] 账单[3] 在位置钞票在同一个阵列[0] 账单[1] 票据[2] 分别为(基本上去掉了备份第一个元素,然后向左移动所有元素)的中终于在复制数组末尾的第一个元素与账单[大小1] = firstElement 。有关详细信息,如何arrayCopy作品此处看到和javadoc以上链接中给出。

您将获得以下内容:

  [2,5,7,1] [2,7,14,15]
[5,7,1,2]:[5,12,13,15]
[7,1,2,5]:[7,8,10,15]
并[1,2,5,7]:[1,3,8,15]

下面是code样品,

进口java.util.Arrays中;

 公共类SumArray {
    公共静态无效的主要(字串[] args){
        INT []票据=新INT [] {2,5,7,1};
        INT大小= bills.length;        的for(int i = 0; I<大小;我++){
            INT []和=新INT [大小]
            对于(INT J = 0; J<大小; J ++){
                如果(J == 0){
                    总和[J] =票据[J]。
                }其他{
                    总和[J] + =总和[J - 1] +票据[J]。
                }
            }            的System.out.println(Arrays.toString(票据)+:+ Arrays.toString(和));            INT firstElement =票据[0];
            System.arraycopy(票据,1,票据,0,大小-1);
            票据[大小1] = firstElement;
        }
    }
}

I'm trying to go through an array:

public static String calculate(int [] bills,int amount){
int size = bills.length;        
int cache = 0;
for (int i = 0; i < size; i++) {
    cache+=bills[i];
    if(cache==amount){
        return "OK";
    }
}
return "NO OK";
}

that would go through the array(and sum) in this order:

1 2 5 7 = 1,3,8,15 (sums)

But What I would like is this:

1st iteration: 1 2 5 7 = 1,3,8,15

2nd iteration: 2 5 7 1 = 2,7,14,15

3rd iteration: 5 7 1 2 = 5,12,13,15

4th iteration: 7 1 2 5 = 7,8,10,15

Note: I'm calling 1st,2nd,3rd,4th since I'm referring to the case of 2 loops; and technically what I want to do is that from the 2nd iteration and so on when it reaches index[3] it goes then to index[0] and and add them up. then start from index[2] and when getting index[3] it sums index[0] and [1].

I tried it on different ways using a double for(and j=i) and when the j(of the 2nd for) reached to last postion of array ([3] in this case) change it to zero and the size=i(which would be the point it started) but it didn't work properly... also maybe it could work with (% modulus) but I would like to see other people's ideas which always helps on learning :) thanks guys!

PS: I used this one as array(ignore the amount part of the method since is not part of the question):

int[] bills = new int[] {1,2,5,7};

PSS: This is the code I tried (with the double loops and it wasn't the one with less mistakes I made because I missed it since I deleted and changed a lot trying new things and this one might have horrible mistakes..):

    public static String calculate(int [] bills,int amount){

    int size = bills.length;        
    int cache = 0;
    int c = 1;
    int z = size-1;
    int s = 0;
    int w = 0;
    boolean TIME = false;

        for (int i = w; i < size;) {

            for (int j = i; j < size;j++){ 
                cache+=bills[j];
                if(cache==amount){
                    cache=0;
                    return "OK";
                }
                System.out.println(TIME);
                if(i>0 && j==z && TIME){
                    size=i;
                    i=0;
                    TIME=false;
                }
            }
            w++;
            size=z+1;
            cache=0;
            TIME=true;
        }
}

解决方案

Here is a simple logic that does that. You can add a new array and then as you loop through your bills array, you can add the sum of the index plus previous one to the new array, print the array and then re-order the numbers of the bills array and repeat the loop.

You can use the System.arrayCopy to change the order of elements in your bills array. Let's assume you have

bills > [2, 5, 7, 1] : sum > [2, 7, 14, 15]

Now you need to rotate your bills array, so the follow does so using arrayCopy

int firstElement = bills[0];
System.arraycopy(bills, 1, bills, 0, size-1);
bills[size-1] = firstElement;

Above three lines, copy the first element of array, then use arrayCopy to copy bills[1], bills[2], bills[3] to the same array in position bills[0], bills[1], bills[2] respectively (basically remove backing up first element and then shifting all elements to the left) an the finally copying the first element at the end of array with bills[size-1] = firstElement. For more details on how arrayCopy works see here and javadoc link is given above.

You will get the following:

[2, 5, 7, 1] : [2, 7, 14, 15]
[5, 7, 1, 2] : [5, 12, 13, 15]
[7, 1, 2, 5] : [7, 8, 10, 15]
[1, 2, 5, 7] : [1, 3, 8, 15]

Here is the code sample,

import java.util.Arrays;

public class SumArray {
    public static void main(String[] args) {
        int[] bills = new int[] { 2, 5, 7, 1 };
        int size = bills.length;

        for (int i = 0; i < size; i++) {
            int[] sum = new int[size];
            for (int j = 0; j < size; j++) {
                if (j == 0) {
                    sum[j] = bills[j];
                } else {
                    sum[j] += sum[j - 1] + bills[j];
                }
            }

            System.out.println(Arrays.toString(bills) + " : " + Arrays.toString(sum));

            int firstElement = bills[0];
            System.arraycopy(bills, 1, bills, 0, size-1);
            bills[size-1] = firstElement;
        }
    }
}

这篇关于通过数组去开始与不同的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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