[英]i got Runtime Error for my code ,can anyone help me to find out where i am wrong
i am getting Runtime Error - NZEC for my code given below.I am not been able to understand why.Can anyone help me out with that. 我在下面给出的代码中遇到运行时错误-NZEC。我无法理解为什么。有人可以帮我解决这个问题。
problem statement: Various signal towers are present in a city.Towers are aligned in a straight horizontal line(from left to right) and each tower transmits a signal in the right to left direction.Tower A shall block the signal of Tower B if Tower A is present to the left of Tower B and Tower A is taller than Tower B. So,the range of a signal of a given tower can be defined as : 问题陈述:城市中存在各种信号塔,塔沿水平线排列(从左到右),并且每个塔都从右向左传输信号。如果塔是A,则塔A将阻止塔B的信号A位于塔B的左侧,塔A比塔B高。因此,给定塔的信号范围可以定义为:
{(the number of contiguous towers just to the left of the given tower whose height is less than or equal to the height of the given tower) + 1}. {(位于给定塔的左侧,其高度小于或等于给定塔的高度的连续塔的数量)+1}。
You need to find the range of each tower. 您需要找到每个塔的范围。
INPUT INPUT
First line contains an integer T specifying the number of test cases. 第一行包含一个整数T,它指定测试用例的数量。
Second line contains an integer n specifying the number of towers. 第二行包含一个整数n,用于指定塔数。
Third line contains n space separated integers(H[i]) denoting the height of each tower. 第三行包含n个以空格分隔的整数(H [i]),表示每个塔的高度。
OUTPUT OUTPUT
Print the range of each tower (separated by a space). 打印每个塔的范围(以空格分隔)。
Constraints 约束
1 <= T <= 10 1 <= T <= 10
2 <= n <= 10^6 2 <= n <= 10 ^ 6
1 <= H[i] <= 10^8 1 <= H [i] <= 10 ^ 8
my code 我的密码
import java.util.*;
import java.io.*;
class TestClass {
public static void main(String args[] ) throws Exception {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
Scanner sc = new Scanner(System.in);
while(t!=0)
{
int n=Integer.parseInt(br.readLine());
int arr[]=new int[n-1];
int atest[]=new int[n-1];
for (int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
for(int j=n-1;j>=0;j--)
{
int count=0;
int temp=j-1;
while(arr[j]>=arr[temp])
{
count++;
temp--;
}
atest[j]=count;
}
for(int k=0;k<n;k++)
{
System.out.println(atest[k]+1);
}
t--;
}
}
}
int arr[]=new int[n-1];
int atest[]=new int[n-1];
for (int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
because array length is n-1 (arr.length = n-1) 因为数组长度是n-1(arr.length = n-1)
that is why you're getting ArrayIndexOutOfBoundsException. 这就是为什么您得到ArrayIndexOutOfBoundsException的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.