奇偶排序:
输入一组乱序的数,先取出奇数从大到小排序,然后取出偶数从小到大排序输出。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
String[] nums = input.split(" ");
int[] numbers = new int[nums.length];
int m = 0, n = 0;
for (int i = 0; i < nums.length; i++) {
numbers[i] = Integer.parseInt(nums[i]);
if (numbers[i] % 2 == 0) {
n++;
} else {
m++;
}
}
int[] mnums = new int[m];
int[] nnums = new int[n];
int p = 0, q = 0;
for (int i = 0; i < nums.length; i++) {
numbers[i] = Integer.parseInt(nums[i]);
if (numbers[i] % 2 == 0) {
nnums[q] = numbers[i];
q++;
} else {
mnums[p] = numbers[i];
p++;
}
}
System.out.println(getResult(mnums, nnums));
}
public static String getResult(int[] mnums, int[] nnums) {
for (int i = 0; i < mnums.length - 1; i++) {
for (int j = i + 1; j < mnums.length; j++) {
if (mnums[i] < mnums[j]) {
int temp = mnums[i];
mnums[i] = mnums[j];
mnums[j] = temp;
}
}
}
for (int i = 0; i < nnums.length - 1; i++) {
for (int j = i + 1; j < nnums.length; j++) {
if (nnums[i] > nnums[j]) {
int temp = nnums[i];
nnums[i] = nnums[j];
nnums[j] = temp;
}
}
}
String result = "";
for (int i = 0; i < mnums.length; i++) {
result = result + " " + mnums[i];
}
for (int i = 0; i < nnums.length; i++) {
result = result + " " + nnums[i];
}
return result.trim();
}
}