如何从给定数组中打印负数子阵列的数量? [英] How do I print the number of negative subarrays from a given array ?

查看:88
本文介绍了如何从给定数组中打印负数子阵列的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题陈述:



给定n个整数数组,在新行上查找并打印其负数子阵列数。(如果子元素的总和为负,则子数为负。)



样本输入



5

1 -2 4 -5 1



样品输出



9



我之前的代码丢了(编辑前)



Problem Statement:

Given an array of n integers, find and print its number of negative subarrays on a new line.(A subarray is negative if the total sum of its elements is negative.)

Sample Input

5
1 -2 4 -5 1

Sample Output

9

My code threw previously (before editing)

Quote:

线程中的异常mainjava.lang.ArrayIndexOutOfBoundsException:5

at Solution。 main(Solution.java:22)

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Solution.main(Solution.java:22)





编辑后代码的当前输出是。





The present output for the code after edit is.

Quote:

输入(stdin)

5

1 -2 4 -5 1

您的输出(标准输出)

7

预期输出

9

编译器消息

错误答案

Input (stdin)
5
1 -2 4 -5 1
Your Output (stdout)
7
Expected Output
9
Compiler Message
Wrong Answer





我哪里错了?



这是我的代码

编辑后< br $> b $ b



Where am I going wrong ?

This is my code
AFTER THE EDIT

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int a[] = new int[n];
        int b[] = new int[n];
        int count=0;
        int i,j,sum = 0;
        for(i=0;i<n;i++)
        {
            a[i] = scan.nextInt();
        }
        for(i=0;i<n;i++)
        {
            if(a[i]<0){count++;}
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                sum = a[i] + sum;
                b[j] = sum;
            }
        }
        for(j=0;j<n;j++)
        {
            if(b[j]<0){count++;}
        }
        System.out.println(count);

    }
}

推荐答案

sum = a[i]+a[i+1];



如果 i 指向数组的最后一个元素,那么 i + 1 将给出例外。您需要找到一种更好的方法来扫描数组中的负值。


If i points to the last element of the array then i + 1 will give the exception. You need to find a better method of scanning your array for negative values.


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

http: //docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your -first-java-application.html [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较做。

当代码没有达到预期的效果时,你就接近一个bug。



注意你做的事情总和

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Pay attention to what you do with sum:
sum = a[i] + sum;





建议:拿一张纸并尝试手工完成,你的程序应该使用相同的程序。



Advice: take a sheet of paper and try to do it by hand, your program should use the same procedure.


从上面的答案我已经对我的代码进行了更改,现在工作正常。谢谢。

这是代码。



From the above answers I have made changes to my code and it is working fine now. Thanks.
Here is the code.

import java.util.*;
public class Solution {

	  public static void main(String[] args) {
	        Scanner scan = new Scanner(System.in);
	        int n = scan.nextInt();
	        int a[] = new int[n];
	        int count=0;
	        int i,j,sum = 0;
	        for(i=0;i<n;i++)
	        {
	            a[i] = scan.nextInt();
	        }
	        scan.close();
	        for(i=0;i<n;i++)
	        {
	        	sum = 0;
	            for(j=i;j<n;j++)
	            {
	                sum = a[j] + sum;
	                if(sum<0){
		                count++;
	                }
	            }
	        }
	        System.out.println(count);
	    }
	}


这篇关于如何从给定数组中打印负数子阵列的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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