您的位置:首页 > 编程语言 > Java开发

java 华为机试题目-数组处理

2014-01-24 00:00 405 查看
给定一个数组input[] ,如果数组长度n为奇数,则将数组中最大的元素放到 output[] 数组最中间的位置,如果数组长度n为偶数,则将数组中最大的元素放到 output[] 数组中间两个位置偏右的那个位置上,然后再按从大到小的顺序,依次在第一个位置的两边,按照一左一右的顺序,依次存放剩下的数。 例如:input[] = {3, 6, 1, 9, 7} output[] = {3, 7, 9, 6, 1}; input[] = {3, 6, 1, 9, 7, 8} output[] = {1, 6, 8, 9, 7, 3} 函数接口 void sort(int input[[, int n, int output[])
package com.duapp.itfanr;

public class CharDemo {
public static void main(String args[]) {
int input1[] = { 3, 6, 1, 9, 7 };
int input2[] = { 3, 6, 1, 9, 7, 8 };
// int len = input1.length ;
// bubbleSort(input1);
// sort(input1, len , output) ;

int len = input2.length;
int[] output = new int[len];
bubbleSort(input2);
sort(input2, len, output);

for (int i = 0; i < len; i++)
System.out.println(output[i]);

}

static void sort(int input[], int n, int output[]) {

int middle;
if (n % 2 == 1) {

middle = (n - 1) / 2;
} else {
middle = n / 2;
}

output[middle] = input[0];

int i = 1, j = 1, ii = 1;
while (true) {
if (ii == n)
break;
else {
output[middle - i] = input[ii];
i++;
ii++;
if (ii == n)
break;
else
output[middle + j] = input[ii];
j++;
ii++;
}
}

}

static void bubbleSort(int input[]) {

int len = input.length;
for (int i = 1; i < len; i++) {
for (int j = 0; j < len - i; j++) {

if (input[j] < input[j + 1]) {
int temp = input[j];
input[j] = input[j + 1];
input[j + 1] = temp;
}

}

}

}

}
参考: [1].
http://blog.csdn.net/yuan22003/article/details/6779622
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 机试