把一個整數陣列中所有的偶數按升序進行排序,所有奇數按降序進行排序,要求奇數和偶數
原有的位置不改變。
例如:陣列int a[5]={100,80,7,44,17}
進行排序后的結果為:
{44,80 ,17,100,7}
排序之前偶數在0、1和3位,排序之后偶數仍在0、1和3位。
排序之前奇數在2和4位,排序之后奇數仍在2和4位。
public class Csort {
public void upbubble(int a[], int n) {
for (int i = 0; i < n - 1; i++) {
boolean flag = true;
for (int j = 0; j < n - 1 - i; j++) {
if (a[j] > a[j + 1]) {
int temp;
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
flag = false;
}
}
if (flag)
break;
}
}
public void downbubble(int a[], int n) {
for (int i = 0; i < n - 1; i++) {
boolean flag = true;
for (int j = 0; j < n - 1 - i; j++) {
if (a[j] < a[j + 1]) {
int temp;
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
flag = false;
}
}
if (flag)
break;
}
}
public void jobubble(int a[], int n) {
// 完成此函式
}
public void display(int a[], int n) {
System.out.println();
for (int i = 0; i < n; i++)
System.out.print(a[i] + " ");
System.out.println();
}
}
public class Cmain {
public static void main(String[] args) {
int a[];
a = new int[20];
a[0] = 50;
a[1] = 17;
a[2] = 2;
a[3] = 34;
a[4] = 401;
a[5] = 157;
a[6] = 15;
a[7] = 81;
a[8] = 52;
a[9] = 63;
a[10] = 64;
a[11] = 95;
a[12] = 51;
a[13] = 82;
a[14] = 33;
a[15] = 65;
a[16] = 911;
a[17] = 22;
a[18] = 83;
a[19] = 104;
Csort BB = new Csort();
System.out.println("原始陣列為:");
BB.display(a, 20);
System.out.println("奇數降序偶數排序后結果為:");
BB.jobubble(a, 20);
BB.display(a, 20);
}
}
這個題目要怎么寫啊 求

uj5u.com熱心網友回復:
第一次遍歷獲取奇數偶數的位置資訊 比如你說的 x= [0,0,1,0,1] 0代表偶數 1代表奇數 。并獲得新的陣列 a[100,80,44] b[17,7].分別排序 a=[44,80,100] b=[17,7]
第二次遍歷x 并獲取 a或者 b的 當前位置資料,a和b的下標分別計算
uj5u.com熱心網友回復:
謝謝public void jobubble(int a[], int n) {
// 完成此函式
int[] x=new int[n];
int[] arr1=new int[n];
int[] arr2=new int[n];
for(int i=0;i<n;i++) {
if(a[i]%2==0) {
x[i]=0;
arr1[i]=a[i];
}
else {
x[i]=1;
arr2[i]=a[i];
}
}
for (int i = 0; i < n; i++)
System.out.print(x[i] + " ");
System.out.println();
for (int i = 0; i < n; i++)
System.out.print(arr1[i] + " ");
System.out.println();
for (int i = 0; i < n; i++)
System.out.print(arr2[i] + " ");
}
然后怎樣將奇數偶數分開,放到兩個陣列中,我分開后得到的結果是這樣的
0 1 0 0 1 1 1 1 0 1 0 1 1 0 1 1 1 0 1 0
50 0 2 34 0 0 0 0 52 0 64 0 0 82 0 0 0 22 0 104
0 17 0 0 401 157 15 81 0 63 0 95 51 0 33 65 911 0 83 0
好像沒法實作這個功能,應該怎么做啊
uj5u.com熱心網友回復:
public static void main(String[] args) {Integer[] a = new Integer[20];
a[0] = 50;
a[1] = 17;
a[2] = 2;
a[3] = 34;
a[4] = 401;
a[5] = 157;
a[6] = 15;
a[7] = 81;
a[8] = 52;
a[9] = 63;
a[10] = 64;
a[11] = 95;
a[12] = 51;
a[13] = 82;
a[14] = 33;
a[15] = 65;
a[16] = 911;
a[17] = 22;
a[18] = 83;
a[19] = 104;
System.out.println(JSONUtil.toJsonStr(jobubble(a)));
}
public static Integer[] jobubble(Integer x[]) {
// 完成此函式
List<Integer> a = new ArrayList<>();
List<Integer> b = new ArrayList<>();
for(int i: x) {
if(i%2 ==0) {
//偶數
a.add(i);
} else {
b.add(i);
}
}
Collections.sort(a);
Collections.sort(b);
int aIndex = 0;
int bIndex = b.size() -1;
for(int i = 0; i < x.length; i++) {
if(x[i]%2 ==0) {
//偶數
x[i] = a.get(aIndex);
aIndex ++;
} else {
x[i] = b.get(bIndex);
bIndex --;
}
}
return x;
}
uj5u.com熱心網友回復:
首先,我覺得你得先確定要使用什么排序方法進行排序,根據排序基本方法進行變更調整。比如說比較常見的冒泡排序。
和一般的冒泡排序相比,這個排序的特點是每次冒泡的奇偶是確定的。
同樣對該陣列進行冒泡排序,但是再每次冒泡之前,判定一下本次冒泡排序是對“奇數冒泡(升序冒泡)”或是對“偶數冒泡(降序冒泡)”(這根據最后冒泡到的位數的奇偶決定)
我覺得這種思路應該可以滿足你的需求。
如果你需要代碼,我可以再單獨貼出
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/276033.html
標籤:Java相關
上一篇:jsp檔案無法運行HTTP Status 500 - servlet可以運行
下一篇:spring 加載 logback.xml 出現 n 不能夠加載cvc-elt.1: 找不到元素 'configuration' 的宣告
