第1个回答 2012-10-22
import java.util.Random;
public class MAX_RAD{
/*
生成numb个随机数组
*/
public int[] randArray(int numb){
int[]temp_array=new int[numb];
Random radobj=new Random();
for(int i=0;i<numb;i++){
temp_array[i]=radobj. nextInt(1000)+1;
System.out.println(" i="+ temp_array[i]);
}
return temp_array;
}
/*
发现numb个随机数组中的最大值
*/
public int max_Value(int[]array){
int max=array[0];
for(int i=1;i<array.length;i++){
if(max<array[i]){
max=array[i] ;
}
}
return max;
}
/*
发现numb个随机数组中的最小值
*/
public int min_Value(int[]array){
int min=array[0];
for(int j=1;j<array.length;j++){
if(min>array[j]){
min=array[j] ;
}
}
return min;
}
public static void main(String[]agrs){
MAX_RAD maxclass=new MAX_RAD();
int []test_Array=maxclass.randArray(10);
System.out.println("maxvlue="+maxclass.max_Value(test_Array));
System.out.println("minvlue="+maxclass.min_Value(test_Array));
}
}
算法的效率为n
第2个回答 2012-10-11
/*
* @(#)Test.java 1.0 2012-10-11
*
* Copyright (c) 2007-2012 Shanghai Handpay IT, Co., Ltd.
* 16/F, 889 YanAn Road. W., Shanghai, China
* All rights reserved.
*
* This software is the confidential and proprietary information of
* Shanghai Handpay IT Co., Ltd. ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with Handpay.
*/
/**
* Class description goes here.
*
* @version 1.0 2012-10-11
* @author lfjiang
* @history
*
*/
public class Test {
/**
* @author lfjiang 2012-10-11
* @param args
*/
public static void main(String[] args) {
test();
}
public static void test() {
int[] ary = getRandomIntegerAry(10);
int min = getMin(ary);
int max = getMax(ary);
for (int i : ary) {
System.out.print(i+"\t");
}
System.out.println("\nmin:"+min);
System.out.println("max:"+max);
}
/**
* 获取随机数组
* @author lfjiang 2012-10-11
* @return
*/
public static int[] getRandomIntegerAry(int aryLength) {
int[] randoms = new int[10];
Random random = new Random();
for (int i = 0 ; i < aryLength ; i++) {
randoms[i] = random.nextInt(1001);
}
return randoms;
}
/**
* 获取数组最小值
* @author lfjiang 2012-10-11
* @param ary
* @return
*/
public static int getMin(int[] ary){
int min = 0;
if (ary != null && ary.length > 0 ) {
min = ary[0];
for (int i = 0 ; i < ary.length ; i++) {
min = Math.min(ary[i], min);
}
}
return min;
}
/**
* 获取数组最大值
* @author lfjiang 2012-10-11
* @param ary
* @return
*/
public static int getMax(int[] ary) {
int max = 0;
if (ary != null && ary.length > 0 ) {
max = ary[0];
for (int i = 0 ; i < ary.length ; i++) {
max = Math.max(ary[i], max);
}
}
return max;
}
/**
* 取两者最小值
* @author lfjiang 2012-10-11
* @param a
* @param b
* @return
*/
public static int compareMin(int a,int b) {
return a < b ? a : b;
}
/**
* 取两者最大值
* @author lfjiang 2012-10-11
* @param a
* @param b
* @return
*/
public static int compareMax(int a , int b) {
return a > b ? a : b;
}
}
第3个回答 2012-10-11
public class TestHello {
public static void maxMin(int[] nums){
int temp = 0;
for (int i = 0; i < nums.length-1; i++) {
for (int j = 0; j < nums.length-i-1; j++) {
if(nums[j]>nums[j+1]){
temp = nums[j+1];
nums[j+1] = nums[j];
nums[j] = temp;
}
}
}
System.out.println("Max: " + nums[9]);
System.out.println("Min: " + nums[0]);
}
public static void main(String[] args) {
Random r = new Random();
int[] array = new int[10];
for (int i = 0; i < 10; i++) {
array[i] = r.nextInt(1000);
}
TestHello.maxMin(array);
}
}