steve_yanrun 于 2016.03.03 19:40 提问

`````` class NotMM{
public void NotMM(int arr[]){
int mid = arr[0];
for(int i=0;i<arr.length-2;i++){
if(arr[i+1]>arr[i]&&arr[i+1]<arr[i+2]){
mid = arr[i+1];
}else if(arr[i+1]<arr[i]&&arr[i+1]>arr[i+2]){
mid = arr[i+1];
}else if(arr[i]<arr[i+1]&&arr[i]>arr[i+2]){
mid = arr[i];
}else if(arr[i]>arr[i+1]&&arr[i]<arr[i+2]){
mid = arr[i];
}else{
mid = arr[i+2];
}
System.out.println(mid);
}
}
}

public class Ex1_4 {
public static void main(String[] args) {
NotMM m = new NotMM();
int arr[] = new int[]{2,4,6,1,7,5,3,8};
m.NotMM(arr);
}
}

``````

8个回答

dongfeng9ge      2016.03.04 11:22

qq_20039385   2016.03.03 19:58

gds2014   2016.03.03 20:00

Arrays.sort(arr);
mid=arr[arr.length()/2];

``````
``````
gds2014   2016.03.03 20:00

Arrays.sort(arr);
mid=arr[arr.length()/2];

``````
``````
qq_26946497   2016.03.03 21:00

if(arr.length<3) {
throw new RuntimeException("无法取得匹配结果");
} else {
return Math.min(arr[0],arr[1],arr[2]);
}

luckyliuwenyuan   2016.03.03 21:12

xionglangs   2016.03.04 10:31

qq_26946497   2016.03.03 19:55

steve_yanrun 嗯嗯
2 年多之前 回复