-java算法题

1.

  *已知一个数组int[98],该数组里面存储了0~99共100个数字中的98个,数字不重复,

 * 请用算法算出0~99中缺少的2个数字是哪两个?
 * 要求:数组自己用程序生成,数值介于0~99,相互之间不重复。
package myTest;

import java.util.Arrays;
import java.util.Random;
/**
 * 已知一个数组int[98],该数组里面存储了0~99共100个数字中的98个,数字不重复,
 * 请用算法算出0~99中缺少的2个数字是哪两个?
 * 要求:数组自己用程序生成,数值介于0~99,相互之间不重复。
 * 上传代码的时候将运算结果截图一起提交。
 * @author Administrator
 *
 */

public class FindLost {
	public static void main(String[] args) {
		int[] ary = new int[98];
		//数组默认每个值为0,现在改为-1
		Arrays.fill(ary, -1);
		for(int i=0;i<98;){
			int temp = new Random().nextInt(100);
			if(compare(temp,ary))
				continue;
			ary[i++] = temp;
		}
		System.out.println("装载:"+ Arrays.toString(ary));
		System.out.println("缺少:"+ Arrays.toString(find(ary)));
			
	}
	//比较有没有相同数据
	public static boolean compare(int random,int [] ary){
			for(int j=0;j<ary.length;j++){
				if(random ==ary[j]){
					return true;
				}
			}
			return false;
	}
	//查找缺少的2个数字
	public static int[] find(int[]ary){
		int[] lost = new int[2];
		Arrays.sort(ary);//系统提供的排序
		System.out.println("排序:"+ Arrays.toString(ary));
		//查找方法
		boolean same = false;
		int temp = 0;//仅为了自增用
		for(int i=0;i<100;i++){
			for(int j=0;j<ary.length;j++){
				if(i==ary[j]){
					same =true;
					break;
				}
			}
			if(!same){
				lost[temp++] = i;
			}else{
				same = false;
			}
		}
		return lost;
	}
}

2.随机生成双色球号码

package myTest;
import java.util.Random;
import java.util.Arrays;

public class DoubleBall {
	/**实现随机生成双色球号码: [ 02 22 13 16 18 12 12 ]
	红球 33 个球 (01~33) 取 六
	蓝球 16 个球 (01~16) 取 一
	*/
	public static void main(String[] args) {
		int[] ary = new int[]{	};
		Random r = new Random();
		boolean same = false;
		while(ary.length<6){
			int num = r.nextInt(34);
			for(int j=0;j<ary.length;j++ ){
				if(ary[j] ==num){
					same = true;
					break;
				}
			}
			if(same){
				continue;
			}
			ary = Arrays.copyOf(ary, ary.length+1);
			ary[ary.length-1] = num;
		}

		//最后一位
		ary = Arrays.copyOf(ary, ary.length+1);
		ary[ary.length-1] = r.nextInt(17);
		printView(ary);
	}
	/**
	 * 按照格式打印显示
	 */
	public static void printView(int[] ary){
				System.out.print("[ ");
				for(int i = 0;i<ary.length;i++){
					if(ary[i]<10){
						System.out.print("0"+ary[i]+" ");
					}else{
						System.out.print(ary[i]+ " ");
					}
					
				}
				System.out.println("] ");
	}
}

3.三种排序算法

package myTest;

import java.util.Arrays;

public class Sort {
	public static void main(String[] args) {
		int[] ary = { 8, 5, 2, 1, 6, 4 };
		//bubbleSort(ary);
		//selectionSort(ary);
		insertSort(ary);
		System.out.println(Arrays.toString(ary));
	}

	/** 冒泡排序 大数向后 */
	public static void bubbleSort(int[] ary) {
		for (int i = 0; i < ary.length - 1; i++) {// length-1次,最后一个不用冒了.
			for (int j = 0; j < ary.length - 1 - i; j++) {
				if (ary[j] > ary[j + 1]) {
					int t = ary[j];	ary[j] = ary[j + 1];ary[j + 1] = t;
				}
			}
		}
	}
	/**选择排序  每次选出最小的数放前面 */
	public static void selectionSort(int[] ary) {
		for(int i=0;i<ary.length-1;i++){
			for(int j=i+1;j<ary.length;j++){
				if(ary[i]>ary[j]){
					int t = ary[i];	ary[i] = ary[j];ary[j] = t;
				}
			}
		}
	}
	/**插入排序  */
	public static void insertSort(int[] ary){
		int t,i,j;
		for(i=1;i<ary.length;i++){
			System.out.println(Arrays.toString(ary));
			t = ary[i];
			for(j=i-1;j>=0&&ary[j]>t;j--){
				ary[j+1] = ary[j];
			}
			ary[j+1] = t;
		}
	}
}


4.打印菱形图案

package algorithm50;

import java.util.Scanner;
/**打印图形*/
public class Test19 {
	/**
	   *    
	  ***    
     *****    
    *******    
     *****    
      ***    
       *    */
	public static void main(String[] args) {
		int n;
		Scanner scan = new Scanner(System.in);
		System.out.println("input:");
		n = scan.nextInt();
		for(int i= -(n-1);Math.abs(i)<n;i++	){
			for(int j=0;j<Math.abs(i);j++){
				System.out.print(" ");
			}
			for(int k=0;k<2*(n-Math.abs(i))-1;k++){
				System.out.print("*");
			}

			System.out.println();
		}
	}
}

5.打印乘法口诀

package algorithm50;

public class Test16 {
	//输出9*9 口诀。 
	public static void main(String[] args) {
		for(int i=1;i<10;i++){
			for(int j=1;j<=i;j++){
				System.out.print(j+" * "+i+" = "+i*j+"   ");
			}
			System.out.println();
		}
	}
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值