Java数组编程练习

【练习题】

在一个数组中找出最大和最小值,并输出它们的位置

public class Test1 {
	private static Scanner scan;

	public static void main(String[] args) {
		scan = new Scanner(System.in);
		int p= scan.nextInt();
		System.out.println("输入"+p+"个数字:");
		int arr[] = new int[p];
		for (int k = 0; k <= arr.length-1; k++) {
			arr[k] = scan.nextInt();
		}

		int max = 0, min = 0;
		int i;
		for (i = 0; i <= arr.length-1; i++) {
			if (arr[max] < arr[i])
				max = i;
		
			if (arr[min] > arr[i])
				min = i;
			
		}
		System.out.println(max + " " + arr[max]);
		System.out.println(min + " " + arr[min]);
	}

}

【练习题】

 冒泡法对一个数组排序

public class Test2 {
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int p= reader.nextInt();
		System.out.println("输入"+p+"个数字:");
		int arr[] = new int[p];
		for (int k = 0; k <= arr.length-1; k++) {
			arr[k] = reader.nextInt();
		}
		
		for(int i=0;iarr[j+1]){
					int t=arr[j];
					arr[j]=arr[j+1];
					arr[j+1]=t;
				}
			}
			
		}

		System.out.println("从小到大排序后:");
		for(int k = 0; k <= arr.length-1; k++){
			System.out.print(arr[k]+" ");
		}
		System.out.println(" ");		
	}

}

【练习题】

 选择法对数数组排序

public class Test3 {
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int p= reader.nextInt();
		System.out.println("输入"+p+"个数字:");
		int arr[] = new int[p];
		for (int k = 0; k <= arr.length-1; k++) {
			arr[k] = reader.nextInt();
		}
		
		for(int i=0;iarr[j])
					k=j;			
			if(i!=k){
				int t=arr[i];
				arr[i]=arr[k];
				arr[k]=t;
			}			
		}
					
		System.out.println("排序后:");
		for(int k = 0; k <= arr.length-1; k++){
			System.out.print(arr[k]+" ");
		}
	}

}

【练习题】

 把一个十进制数转换成任意进制的数

public class Test4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner reader = new Scanner(System.in);
		System.out.println("输入:");
		int m = reader.nextInt();
		int n= reader.nextInt();
		char t[]=toAny(m,n);
		printChar(t);
		System.out.println( );
		
	}
	static char[] toAny(int m,int n){
		char[]s =new char[32];
		int k=0;
		while(m>0){
			int t=m%n;
			if(t<10)
				s[k++]=(char)(t+'0');
			else
				s[k++]=(char)(t-10+'A');
			m=m/n;
		}
		char ss[]=new char[k];
		for(int i=k-1;i>=0;i--)
			ss[k-i-1]=s[i];
			return ss;
	}
	
	static void printChar(char s[]){
		if(s==null)  return;
		for(int i=0;i

【练习题】

 实现一个数组的逆序存储

public class Test5 {
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int p= reader.nextInt();
		System.out.println("输入"+p+"个数字:");
		int arr[] = new int[p];
		for (int k = 0; k <= arr.length-1; k++) {
			arr[k] = reader.nextInt();
		}
		
		for (int i=0; i

【练习题】

 在一个有序的数组插入一个数,也保证有序

public class Test6 {
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int p= reader.nextInt();
		System.out.println("输入"+p+"个数字:");
		int arr[] = new int[p];
		for (int k = 0; k < p-1; k++) {
			arr[k] = reader.nextInt();
		}
		
		System.out.println("输入需要插入的数字:");
		int m= reader.nextInt();
		
		System.out.println("插入后的数组为:");					
		int index=arr.length-1;
		for(int i = 0; i < arr.length-1; i++){
		   	if(arr[i]>=m){
		   		index=i;
		   		break;	
		   	}
		}
		for(int i = arr.length-1; i >index; i--){
			arr[i]=arr[i-1];	
		}
		arr[index]=m;
			
		for(int k = 0; k < arr.length; k++){			
			System.out.print(arr[k]+" ");
		}
										
	}

}

【练习题】

在一个有序的数组中,利用折半法进行查找

public class Test7 {
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int p= reader.nextInt();
		System.out.println("输入"+p+"个数字:");
		int arr[] = new int[p];
		for (int k = 0; k <= arr.length-1; k++) {
			arr[k] = reader.nextInt();
		}
		
		for(int i=0;iarr[j+1]){
					int t=arr[j];
					arr[j]=arr[j+1];
					arr[j+1]=t;
				}
			}
			
		}

		System.out.println("从小到大排序后:");
		for(int k = 0; k <= arr.length-1; k++){
			System.out.print(arr[k]+" ");
		}
		System.out.println(" ");
		
		System.out.println("输入需要查找的数字:");
		int m= reader.nextInt();			
				
		int lower=0,upper=arr.length-1;
		int mid=0;		
		while(lower<=upper){	
			mid=(lower+upper)/2;
			if(arr[mid]m)
				upper=mid-1;
			else{
				mid+=1;
				System.out.println("查找的数字为数组的第"+(mid-1)+"项");
				return;			
			}	
		}
		
		for(int i=0;i<=arr.length;i++){
			if(m!=arr[i]){
				System.out.println("查找的数字不存在!");
				return;
			}			
		}
					
	}

}

【练习题】

输入一批数,找出其中位数

public class Test8 {
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int p= reader.nextInt();
		System.out.println("输入"+p+"个数字:");
		double arr[] = new double[p];
		for (int k = 0; k <= arr.length-1; k++) {
			arr[k] = reader.nextInt();
		}
		
		for(int i=0;iarr[j+1]){
					double t=arr[j];
					arr[j]=arr[j+1];
					arr[j+1]=t;
				}
			}			
		}
		
		System.out.println("从小到大排序后:");
		for(int k = 0; k <= arr.length-1; k++){
			System.out.print(arr[k]+" ");
		}
		System.out.println(" ");
		
		if(arr.length%2!=0){
			int mid=(arr.length-1)/2;
			System.out.println("输出其中位数:"+arr[mid]);
		}
		else{
			int mid1=arr.length/2;
			int mid2=mid1-1;
			System.out.println("输出其中位数:"+(arr[mid1]+arr[mid2])/2);
		}	
		
	}

}

 

你可能感兴趣的:(Java基础)