Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 什么';以下更改数组以使其变为非递减的解决方案有何错误?_Java_Arrays_Algorithm - Fatal编程技术网

Java 什么';以下更改数组以使其变为非递减的解决方案有何错误?

Java 什么';以下更改数组以使其变为非递减的解决方案有何错误?,java,arrays,algorithm,Java,Arrays,Algorithm,我期望的不是确切的答案,而是我可以改进解决方案的方法。 目前它显然是错误的,它甚至没有通过一个测试用例。 在一种情况下,它给出了错误的答案,而在其他情况下,它显示超时 问题 给定一个大小为N的数组A,它将被转换成一个数组,这样A[i-1]

我期望的不是确切的答案,而是我可以改进解决方案的方法。 目前它显然是错误的,它甚至没有通过一个测试用例。 在一种情况下,它给出了错误的答案,而在其他情况下,它显示超时

问题

给定一个大小为N的数组A,它将被转换成一个数组,这样A[i-1] 输入

2    // Total Number of Test Cases
2 1    // First value is N and second is X
1 1    // Elements of the first Array
3 1    // First value is N and second is X for the second array
1 1 2  // Elements of the second array
1
2
输入的第一行将包含T(测试用例的数量)。 对于每个测试用例,第一行将包含两个表示N和X的空格分隔整数。下一行将包含表示A[i]的N个空格分隔整数

输出

2    // Total Number of Test Cases
2 1    // First value is N and second is X
1 1    // Elements of the first Array
3 1    // First value is N and second is X for the second array
1 1 2  // Elements of the second array
1
2
对于每个测试用例,在新行中打印所需答案

样本输入

2    // Total Number of Test Cases
2 1    // First value is N and second is X
1 1    // Elements of the first Array
3 1    // First value is N and second is X for the second array
1 1 2  // Elements of the second array
1
2
样本输出

2    // Total Number of Test Cases
2 1    // First value is N and second is X
1 1    // Elements of the first Array
3 1    // First value is N and second is X for the second array
1 1 2  // Elements of the second array
1
2
我的解决方案:-

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;


class TestClass {
    public static void main(String args[] ) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int T = Integer.parseInt(line);// Total number of Test Cases
        int N = 0;// Number of elements in the Array
        int X = 0;// The number which can be added to the elements of the array
        int[] arr;
        int count;// Final count value which needs to be shown
        while(T-->0) {
            String[] str = br.readLine().trim().split("\\s+");
            N= Integer.parseInt(str[0]);
            X = Integer.parseInt(str[1]);
            arr = new int[N];
            str = br.readLine().trim().split("\\s+");
            count = 0;
            for(int i = 0; i < N; i++){
                arr[i] = Integer.parseInt(str[i]);
            }

            for(int i = 1; i < N ; i++){
                while(arr[i - 1] >= arr[i]){
                    arr[i] += X;
                    count++;
                }
            }
            System.out.println(count);
        }


    }
}
导入java.io.BufferedReader;
导入java.io.InputStreamReader;
导入java.util.array;
类TestClass{
公共静态void main(字符串args[])引发异常{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
String line=br.readLine();
int T=Integer.parseInt(行);//测试用例总数
int N=0;//数组中的元素数
int X=0;//可以添加到数组元素中的数字
int[]arr;
int count;//需要显示的最终计数值
而(T-->0){
字符串[]str=br.readLine().trim().split(\\s+);
N=Integer.parseInt(str[0]);
X=Integer.parseInt(str[1]);
arr=新整数[N];
str=br.readLine().trim().split(\\s+);
计数=0;
对于(int i=0;i=arr[i]){
arr[i]+=X;
计数++;
}
}
系统输出打印项次(计数);
}
}
}
对于上述示例输入,上述代码生成的答案是正确的,但在在线法官隐藏的其他情况下失败,并给出超时。 如何解决此问题?

两个潜在问题:

首先,在for循环中有一个while循环。您应该能够直接使用除数而不是循环来计算迭代次数。这样可以避免超时

让我们使用k来表示while循环的迭代次数。 我们知道

A[i]+k*x > A[i-1].  
因此我们可以推断

k > (A[i-1]-A[i])/x 
使用整数算术,循环的迭代次数可计算为:

k = (A[i-1]-A[i]+x)/x if A[i] <= A[i-1], or 0 otherwise

k=(A[i-1]-A[i]+x)/x如果A[i]不知道神秘的输入是什么,没有人能够帮助你。你是对的。我所期望的是通过更正确的方式来改进代码的不同方法。我接受这个答案。我不认为应该进行代码审查,因为解决方案是不正确的,除非它是正确的,否则我不认为会出现审查问题。@AniMenon作为代码审查的主持人,我认为目前这不属于代码审查。如果问题是在线判断失败(即给出了糟糕的结果),那么它就偏离主题了。如果问题仅仅是它给出了超时,那么它就是主题。从解决方案来看是不正确的,因此在代码审查上偏离了主题。他们编辑了这个问题,它最初说,代码工作,但不是对一些神秘的输入,甚至隐藏在OP。最初的问题说,对于上述样本输入,上述代码生成的答案是正确的,但在其他情况下失败,这些情况是由在线法官隐藏的,并且它给出了超时。如何解决这个问题?请详细说明你所说的除法。@JohnDoe我对除法做了更多解释