java 如何求两个零之间的数组元素之和

czfnxgou  于 2022-12-21  发布在  Java
关注(0)|答案(4)|浏览(145)

第一输入:数组大小:5
第二输入:0 -2 4 0 6
产量:2
下面是我尝试过的方法。它也是将零之前的数字相加:
我的代码的输出:
数组大小:10
元素:6 19 0 -3 4 8 0 -6 9 59
我的产出:25 9

import java.util.Scanner;
import java.util.Vector;
public class Main{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int arr[] = new int[n]; 
        Vector<Integer> A = new Vector<Integer>();

    int sum = 0;
        for(int i=0; i<arr.length; i++){
            arr[i] = in.nextInt();
        }
 
   
    for(int i = 0; i < arr.length; i++)
    {
       if (arr[i] == 0)
       {
           i++;
           break;
       }
    }
 
   
    for(int i=0; i < arr.length; i++)
    {
        
    
       if (arr[i] == 0)
       {
           A.add(sum);
           sum = 0;
       }
        
       else
       {
           sum += arr[i];
       }
    }

    for(int j = 0; j < A.size(); j++)
    {
       System.out.print(A.get(j) + " ");
    }
    }
}
vmdwslir

vmdwslir1#

你不需要两个循环,一个寻找第一个零,然后求和。

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int arr[] = new int[n];
        List<Integer> sums = new ArrayList<>();
        int sum = 0;

        // read input
        for (int i = 0; i < arr.length; i++) {
            arr[i] = in.nextInt();
        }

        boolean isCounting = false;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != 0 && isCounting) {
                sum += arr[i];
            } else if(arr[i] == 0){
                // if already counting then finish the sum
                if (isCounting) {
                    sums.add(sum);
                    sum = 0;
                } else { // else start counting
                    isCounting = true;
                }
            }
        }
        System.out.println(sums);
    }
}
lnxxn5zx

lnxxn5zx2#

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int size = in.nextInt();
        int arr[] = new int[size];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = in.nextInt();
        }
        int sum = 0;
        int idx = 0;
        while (arr[idx] != 0)
            idx++; // After this, idx will be the index of the first 0
        do {
            idx++; // Keep looping from idx + 1 until the next 0
            if (arr[idx] != 0 && idx < arr.length)
                sum += arr[idx];
        } while (idx < arr.length && arr[idx] != 0);
        System.out.println("Sum = " + sum);
    }
}
dffbzjpn

dffbzjpn3#

int sum = -1;
int j = -1;
    
    for(int i=0; i<arr.length;i++ ){
       if (arr[i] == 0) {
           j = i;
           if (sum == -1)
               sum = 0;
            else
               break;
       } 
       if ( j == -1)
          continue;
        sum += arr[i];
    }

以上方法应该有效

jmp7cifd

jmp7cifd4#

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int[] arr = new int[n];
    int sum = 0;
    List<Integer> list = new ArrayList<>();

    for (int i = 0; i < n; i++) {
        arr[i] = in.nextInt();
        if (arr[i] == 0) {
            list.add(i);
        }
    }

    for (int i = list.get(0); i <= list.get(list.size()-1); i++) {
        sum += arr[i];
    }

    System.out.println(sum);
}

相关问题