2014.10.26排序与 龟兔赛跑

  1. 简单选择排序
    package com.lovo;
    
    public class Test02 {
    
    	public static void main(String[] args) {
    		int[] a = new int[5];
    		System.out.print("排序前: ");
    		for(int i = 0; i < a.length; i++) {
    			a[i] = (int) (Math.random() * 99 + 1);
    			System.out.print(a[i] + "  ");
    		}
    		// 简单选择排序
    		for(int i = 0; i < a.length - 1; i++) {
    			int minIndex = i;
    			for(int j = i + 1; j < a.length; j++) {
    				if(a[j] < a[minIndex]) {
    					minIndex = j;
    				}
    			}
    			if(minIndex != i) {
    				int temp = a[i];
    				a[i] = a[minIndex];
    				a[minIndex] = temp;
    			}
    		}
    		
    		System.out.print("\n排序后: ");
    		for(int x : a) {
    			System.out.print(x + "  ");
    		}
    	}
    }
    

  2. 冒泡排序
    package com.lovo;
    
    public class Test03 {
    
    	public static void main(String[] args) {
    		int[] a = new int[5];
    		System.out.print("排序前: ");
    		for(int i = 0; i < a.length; i++) {
    			a[i] = (int) (Math.random() * 99 + 1);
    			System.out.print(a[i] + "  ");
    		}
    		// 冒泡排序
    		
    		boolean swapped = true;	// 有没有发生过交换
    		for(int i = 1; swapped && i <= a.length - 1; i++) {
    			swapped = false;	// 表示尚未发生交换
    			for(int j = 0; j < a.length - i; j++) {
    				if(a[j] > a[j + 1]) {
    					int temp = a[j];
    					a[j] = a[j + 1];
    					a[j + 1] = temp;
    					swapped = true;
    				}
    			}
    		}
    		
    		System.out.print("\n排序后: ");
    		for(int x : a) {
    			System.out.print(x + "  ");
    		}
    	}
    }
    

  3. 龟兔赛跑
    package com.hechao;
    /**
     * 乌龟类
     * @author hechao
     *
     */
    public class Tortoise {
    	private String name;
    	private double speed;
    	
    	/**
    	 * 构造器
    	 * @param name
    	 */
    	public Tortoise(String name) {
    		this.name = name;
    	}
    	/**
    	 * 获得名字
    	 * @return
    	 */
    	public String getName() {
    		return name;
    	}
    	/**
    	 * 获得速度
    	 * @return
    	 */
    	public double getSpeed() {
    		return speed;
    	}
    	/**
    	 * 吃药
    	 */
    	public void takeMedicine(){
    		speed *= 100;
    	}
    	/**
    	 * 爬行
    	 */
    	public void creep(){
    		speed = 0.5;
    	}
    	/**
    	 * 魔法
    	 */
    	public void magic(Rabbit ra){
    		ra.sleep();
    	}
    }
    

    package com.hechao;
    /**
     * 兔子类
     * @author hechao
     *
     */
    public class Rabbit {
    	private String name;
    	private double speed;
    	private String result;
    	/**
    	 * 构造器
    	 * @param name
    	 */
    	public Rabbit(String name) {
    		this.name = name;
    	}
    	/**
    	 * 获得名字
    	 * @return
    	 */
    	public String getName() {
    		return name;
    	}
    	/**
    	 * 获得速度
    	 * @return
    	 */
    	public double getSpeed() {
    		return speed;
    	}
    	/**
    	 * 奔跑
    	 */
    	public void run(){
    		speed = 36;
    	}
    	/**
    	 * 睡觉
    	 */
    	public void sleep(){
    		speed = 0;
    	}
    	/**
    	 * 吃蘑菇
    	 */
    	public void eatMushroom(){
    		int temp = (int)(Math.random() * 2);
    		if(temp == 0){
    			speed -= 30;
    			result = "吃了毒蘑菇,速度减慢!";
    		}
    		else{
    			speed += 20;
    			result = "吃了能量蘑菇,速度加快!";
    		}
    	}
    	/**
    	 * 获得结果
    	 * @return
    	 */
    	public String getResult(){
    		return result;
    	}
    }
    
    package com.hechao;
    
    import java.util.Scanner;
    
    public class Test02 {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		Tortoise tor = new Tortoise("忍者龟");
    		Rabbit rab = new Rabbit("流氓兔");
    		System.out.println("比赛距离1000公里");
    		System.out.println("------------------------------");
    		tor.creep();
    		System.out.println(tor.getName() + "初始速度为:" + tor.getSpeed());
    		double torT1 = (int)(Math.random() * 5 + 1);
    		double torS1 = torT1 * tor.getSpeed();
    		System.out.println(torT1 + "小时爬行距离为:" + torS1 + "公里");
    		tor.takeMedicine();
    		System.out.println(tor.getName() + "吃了兴奋剂,爬行速度为:" + tor.getSpeed() + "公里/小时");
    		double torT2 = (int)(Math.random() + 2);
    		double torS2 = torT2 * tor.getSpeed();
    		System.out.println(torT2 + "小时爬行距离为:" + torS2 + "公里/小时");
    		tor.creep();
    		tor.magic(rab);
    		System.out.println(tor.getName() + "对" + rab.getName() + "施了魔法");
    		double torT3 = (1000 - torS1 - torS2) / tor.getSpeed();
    		System.out.println("------------------------------");
    		rab.run();
    		System.out.println(rab.getName() + "初始速度为:" + rab.getSpeed() + "公里/小时");
    		double rabT1 = (int)(Math.random() + 2);
    		double rabS1 = rabT1 * rab.getSpeed();
    		System.out.println(rabT1 + "小时奔跑距离为:" + rabS1 + "公里");		
    		double rabT2 = (int)(Math.random() + 2);
    		rab.eatMushroom();
    		System.out.println(rab.getName() + rab.getResult() + "当前速度为:" + rab.getSpeed());	
    		double rabS2 = rabT2 * rab.getSpeed();
    		System.out.println(rabT2 + "小时奔跑距离为:" + rabS2 + "公里");			
    		rab.sleep();
    		double rabT3 = 12;
    		System.out.println(rab.getName() + "睡觉" + rabT3 + "小时");
    		rab.run();
    		double rabT4 = (int)(Math.random() + 1);
    		double rabS4 = rabT4 * rab.getSpeed();
    		System.out.println(rabT4 + "小时奔跑距离为:" + rabS4 + "公里");
    		double rabT5 = 12;
    		System.out.println(rab.getName() + "被" + tor.getName() + "施了魔法,睡觉" + rabT5 + "小时");
    		rab.run();
    		double rabT6 = (1000 - rabS1 - rabS2 - rabS4) / rab.getSpeed();
    		System.out.println("------------------------------");
    		double time1 = torT1 + torT2 + torT3;
    		double time2 = rabT1 + rabT2 + rabT3 + rabT4 + rabT5 + rabT6; 
    		if(time1 > time2){
    			System.out.println(rab.getName() + "获胜!");
    		}
    		if(time1 < time2){
    			System.out.println(tor.getName() + "获胜!");
    		}
    		if(time1 == time2){
    			System.out.println(rab.getName() + "与" + tor.getName() + "同时到达,未分胜负!");
    		}
    		sc.close();
    	}
    }
    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值