英文:
How do you only order odd numbers in an int array and leave even numbers in their original position?
问题
基本上,有一个在线挑战,你需要对数组中的奇数进行升序排序,同时忽略偶数。例如,[3,1,4,2,3,6] 应该变成 [1,3,4,2,3,6]。我已经编写了一个算法来对数字进行排序,但输出是错误的。
import java.util.Arrays;
public class Kata {
public static int[] sortArray(int[] array) {
int n;
int temp = 0;
if (array.length < 2){
System.out.println(Arrays.toString(array));
return array;
}
for (n = 1; n < array.length; n++) { // 遍历数组
if (array[n] % 2 != 0 && array[n] > array[n-1] && array[n-1] % 2 != 0){ // 如果 n 是奇数且比 n-1 大,而且 n-1 也是奇数
temp = array[n];
array[n] = array[n-1];
array[n-1] = temp;
}
else if (array[n] % 2 == 0 && array[n] < array[n-1]){
array[n] = array[n];
}
else if (array[n] % 2 != 0 && array[n] > array[n-1]){
array[n] = array[n];
}
}
System.out.println(Arrays.toString(array));
return array;
}
}
驱动代码
public class Driver {
public static void main(String[] args) {
Kata.sortArray(new int[]{3,1,11,2,9});
}
}
对于上面的数组,期望输出应该是:[1,3,9,2,11],但实际输出却是 [3, 11, 1, 2, 9]。我已经盯着这个问题看了很久,完全想不通。非常感谢阅读!
英文:
Basically, there is this challenge online where you have to order the odd numbers in an array in ascending order, while also ignoring the even numbers. So for example, [3,1,4,2,3,6] would be -> [1,3,4,2,3,6]. I've written an algorithm to sort the numbers, but the output is wrong.
import java.util.Arrays;
public class Kata {
public static int[] sortArray(int[] array) {
int n;
int temp = 0;
if (array.length < 2){
System.out.println(Arrays.toString(array));
return array;
}
for (n = 1; n < array.length; n++) { // iterates through array
if (array[n] % 2 != 0 && array[n] > array[n-1] && array[n-1] % 2 != 0){ // if n is odd and smaller than n-1 and n-1 is odd
temp = array[n];
array[n] = array[n-1];
array[n-1] = temp;
}
else if (array[n] % 2 == 0 && array[n] < array[n-1]){
array[n] = array[n];
}
else if (array[n] % 2 != 0 && array[n] > array[n-1]){
array[n] = array[n];
}
}
System.out.println(Arrays.toString(array));
return array;
}
}
Driver code
public class Driver {
public static void main(String[] args) {
Kata.sortArray(new int[]{3,1,11,2,9});
}
}
for the array above, the output should be: [1,3,9,2,11] but instead it's [3, 11, 1, 2, 9]. I've been staring at this for ages and I'm stumped, honestly. Thanks a lot for reading!
答案1
得分: 2
我使用了一个内循环来查找下一个要排序的奇数。而且我使用了额外的外循环,因为通常排序需要两个循环。
import java.util.Arrays;
public class Kata {
public static int[] sortArray(int[] array) {
int i;
int n;
int m;
int temp = 0;
if (array.length < 2) {
System.out.println(Arrays.toString(array));
return array;
}
for (i = 0; i < array.length; i++) {
for (n = 0; n < array.length; n++) {
if (array[n] % 2 != 0) {
for (m = n + 1; m < array.length; m++) {
if (array[m] % 2 != 0) {
if (array[n] > array[m]) {
temp = array[n];
array[n] = array[m];
array[m] = temp;
}
break;
}
}
}
}
}
System.out.println(Arrays.toString(array));
return array;
}
}
英文:
I used an inner loop to find the next odd number to sort. And I used an additional outer loop because normally sorting needs two loops.
import java.util.Arrays;
public class Kata {
public static int[] sortArray(int[] array) {
int i;
int n;
int m;
int temp = 0;
if (array.length < 2) {
System.out.println(Arrays.toString(array));
return array;
}
for (i = 0; i < array.length; i++) {
for (n = 0; n < array.length; n++) {
if (array[n] % 2 != 0) {
for (m = n + 1; m < array.length; m++) {
if (array[m] % 2 != 0) {
if (array[n] > array[m]) {
temp = array[n];
array[n] = array[m];
array[m] = temp;
}
break;
}
}
}
}
}
System.out.println(Arrays.toString(array));
return array;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论