用另一个数组中的元素替换几乎有序数组中的元素。

huangapple go评论41阅读模式
英文:

Replacing element in an almost sorted array with an element from another array

问题

我的第一个数组几乎已经排序好了。我需要用第二个数组中可能的最大元素替换未排序的元素。错误放置的元素不会在第0个或第n-1个索引上。如果array1[i]小于array1[i-1],那么索引i是错误的索引。

例如:
第一个输入 5
第一个数组:2 7 8 6 13
第二个输入 4
第二个数组 15 11 9 5
我的结果将是 11

但是,如果没有可能从第二个数组中取得的元素,它将打印 "not possible",例如:14 15 16 17,或者如果它有0个元素。

如何修复这段代码?

int n1 = s.nextInt();
int[] array1 = new int[n1];
for (int i = 0; i < n1; i++) {
    array1[i] = s.nextInt();
}

int n2 = s.nextInt();
int[] array2 = new int[n2];
for (int i = 0; i < n2; i++) {
    array2[i] = s.nextInt();
}

int temp1 = 0;
int temp2 = 0;

for (int i = 0; i < array1.length - 1; i++) {
    if (array1[i] > array1[i + 1]) {
        temp1 = array1[i + 1];
        temp2 = array1[i + 2];
        break;
    }
}

int temp3 = 0;
for (int j = 0; j <= array2.length - 1; j++) {
    if (array2[j] > temp1 && array2[j] < temp2) {
        temp3 = array2[j];
        break;
    }
}
System.out.println(temp3);
英文:

My first array is almost sorted. I need to replace the unsorted element with the max possible element from the second array. The wrongly placed element would not be on the 0th or n-1 index. And if array1[i] is less than array1[i-1], then index i is the wrong index.

For ex:
first input 5
first array: 2 7 8 6 13
second input 4
second array 15 11 9 5
My result would be 11

however, If there is no possible element I could take from the second array, it would print not possible. for instance : 14 15 16 17 or if it has 0 element.

How can I fix this code?

   int n1 = s.nextInt();
   int[] array1 = new int[n1];
   for (int i = 0; i &lt; n1; i++) {
       array1[i] = s.nextInt();
   }
   
   int n2 = s.nextInt();
   int[] array2 = new int[n2];
   for (int i = 0; i &lt; n2; i++) {
       array2[i] = s.nextInt();
   }
  
   
    int temp1=0;
    int temp2=0;
 
    
    for(int i=0; i &lt; array1.length-1;i++) {
       if (array1[i] &gt; array1[i+1]) {
           temp1=array1[i+1];
           temp2=array1[i+2];
           break;
       }
       
   }
   
int temp3 = 0;
   for(int j=0; j&lt;=array2.length-1;j++) {
       if(array2[j] &gt; temp1 &amp;&amp; array2[j] &lt; temp2){
           temp3 = array2[j];
           break;
       }
   }
   System.out.println(temp3);

   }

}

答案1

得分: 2

对于你提供的代码,我将只翻译注释和注释中的文本。以下是翻译后的代码部分:

// "static void main" 必须在一个公共类中定义。
public class Main {
    public static void main(String[] args) {
      
   Scanner s = new Scanner(System.in);      
        
   int n1 = s.nextInt();
   s.nextLine();
   int[] array1 = new int[n1];
   for (int i = 0; i &lt; n1; i++) 
       array1[i] = s.nextInt();
   s.nextLine();
   
   int n2 = s.nextInt();
   s.nextLine();
   int[] array2 = new int[n2];
   for (int i = 0; i &lt; n2; i++) 
       array2[i] = s.nextInt();
   s.nextLine();
  
   
    int temp1=0;
    int temp2=0;
 
    // 执行一些更改,如下所示,对于上述问题中提到的测试用例有效...
    // 1. 假设索引 i+1 处的值违反了排序顺序,则应取 temp1 = i 和 temp2 = i+2。
    for(int i=0; i &lt; array1.length-1;i++) 
       if (array1[i] &gt; array1[i+1]){ 
           temp1=array1[i];
           temp2=array1[i+2];
       }   

   int temp3 = -1;
   // 2. 找到在 temp1 和 temp2 之间的数组2中的最大值。
   for(int j=0; j&lt;array2.length-1;j++) 
       if(array2[j] &gt; temp1 &amp;&amp; array2[j] &lt; temp2)
           if(array2[j]&gt;temp3)
               temp3 = array2[j];

    if(temp3 == -1)
        System.out.println("不可能\n");
    else
        System.out.println(temp3);

    }
}

请注意,我只翻译了注释和注释中的文本,而没有翻译代码本身。

英文:

Performing Some changes in your code as shown below, it worked for the test cases mentioned in the problem above...

1. Suppose the value at index i+1 violates the sorted order then you should take temp1 = i and temp2 = i+2.

2. Find the maximum value in array2 which lies between temp1 and temp2.

// &quot;static void main&quot; must be defined in a public class.
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);      
int n1 = s.nextInt();
s.nextLine();
int[] array1 = new int[n1];
for (int i = 0; i &lt; n1; i++) 
array1[i] = s.nextInt();
s.nextLine();
int n2 = s.nextInt();
s.nextLine();
int[] array2 = new int[n2];
for (int i = 0; i &lt; n2; i++) 
array2[i] = s.nextInt();
s.nextLine();
int temp1=0;
int temp2=0;
for(int i=0; i &lt; array1.length-1;i++) 
if (array1[i] &gt; array1[i+1]){ 
temp1=array1[i];
temp2=array1[i+2];
}   
int temp3 = -1;
for(int j=0; j&lt;array2.length-1;j++) 
if(array2[j] &gt; temp1 &amp;&amp; array2[j] &lt; temp2)
if(array2[j]&gt;temp3)
temp3 = array2[j];
if(temp3 == -1)
System.out.println(&quot;Not Possible\n&quot;);
else
System.out.println(temp3);
}
}

huangapple
  • 本文由 发表于 2020年8月3日 01:45:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63219244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定