论坛首页 Java企业应用论坛

国王和100个犯人-

浏览 25932 次
该帖已经被评为新手帖
作者 正文
   发表时间:2010-01-15  
就没人看看我的代码, 有人仔细分析了一下没
0 请登录后投票
   发表时间:2010-01-15  
hrwhat 写道
zj1211 写道
先支持一下楼主,至少楼主能在编码中找到快乐。 顺便感叹一下,这年头直接使用JDK的提供的方法怎么就忒受人歧视,有必要言必称希腊吗。

  建议以后放屁还是先脱裤子,再用食品袋罩住屁股。这样才是放屁经验超过1年的高手。因为:
1 比较环保,不会污染空气。
2 不会污染裤子,也就减少洗衣服的次数,也就等于为减少C排放做贡献
3 拉动了塑料袋的消费,为GDP增长做贡献

如此多的好处啊。



支持,就这么个小程序,用得着上纲上线吗?
重要的是心态,乐观,积极向上的心态最重要


你说的对,心态很重要
有时面试会出这种小问题,让你写代码,高手就从这一小段代码就能看出你的水平哦
0 请登录后投票
   发表时间:2010-01-15  
中发白 写道
就没人看看我的代码, 有人仔细分析了一下没

有点疑惑为什么属性都不加权限修饰符呢?
0 请登录后投票
   发表时间:2010-01-15  
168_ccxx 写道
100行不到,包括空行等
//灯类
public class Light
{
    private boolean isOn; //开关 默认开

    public Light()
    {
        this.isOn = true;
    }

    public boolean isOn()
    {
        return isOn;
    }

    public void setOn(boolean isOn)
    {
        this.isOn = isOn;
    }
}

//囚犯,不包括计数员
public class Prisoner
{
    private boolean isOnLight; //是否关过灯

    public Prisoner()
    {
        this.isOnLight = false;
    }

    public boolean isOnLight()
    {
        return isOnLight;
    }

    public void setOnLight(boolean isOnLight)
    {
        this.isOnLight = isOnLight;
    }
}

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class StartGame
{
    private static Map<Integer, Prisoner> pMap = new HashMap<Integer, Prisoner>();

    private static Random rd = new Random();

    static
    {
        Prisoner ps;
        for (int i = 0; i < 99; i++) //第99的位置留给计数员
        {
            ps = new Prisoner();
            pMap.put(i, ps);
        }
    }

    public static void main(String[] args)
    {
        int count = 0;
        int dayCount = 0;
        Light lt = new Light();
        while (true)
        {
            dayCount++;
            int num = rd.nextInt(100);
            if (num == 99)
            {
                lt.setOn(true);
                continue;
            }
            Prisoner ps = pMap.get(num);
            if (!ps.isOnLight() && lt.isOn())
            {
                count++;
                ps.setOnLight(true);
                lt.setOn(false);
            }
            if (count == 99)
            {
                break;
            }
        }
        System.out.println(dayCount);
        System.out.println(dayCount / 365);
    }
}


算法不对,没看清楼住的题目
0 请登录后投票
   发表时间:2010-01-15  
中发白 写道
刚看了一下需求, 自己也就琢磨写了一下, 后面再看了楼主和楼下的, 深感自卑。
指点一下我的缺点。本人工作一年
犯人类
public class Prisoner {

	int id; //犯人编号

	boolean state = false; //犯人状态, 放过风的true, 没放过风的为false;
	
	public Prisoner(){}
	
	public Prisoner(int id){
		this.id = id;
	}
	
	/**
	 * @param light
	 * 开灯
	 */
	public void turnOn(Light light){
		light.turnOn();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public boolean isState() {
		return state;
	}

	public void setState(boolean state) {
		this.state = state;
	}
	
}

//犯人计数人员
public class PrisonCounter extends Prisoner {
	
	Counter count;
	
	public PrisonCounter(int id, Counter count){
		this.id = id;
		this.count = count;
	}
	
	/**
	 * @param light
	 * 关灯
	 */
	public void turnOff(Light light){
		light.turnOff();
	}
	
	public void addCount(){
		this.count.addCount();
	}
	
	public int getCount(){
		return this.count.getCount();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
//灯
public class Light {

	boolean isLight = false;
	
	public void turnOn() {
		isLight = true;
	}

	public void turnOff() {
		isLight = false;
	}
	
	public boolean isLight(){
		return isLight;
	}

}

//计数器
public class Counter {
	
	private int count = 0;

	public void addCount() {
		count++;
	}

	public int getCount() {
		return this.count;
	}

}

//随机计数人员
public class RandomPointPrisonCount {
	
	int id;

	public RandomPointPrisonCount(Prisoner[] prisonArray){
		Random rd = new Random();
		int id = rd.nextInt(prisonArray.length);
		this.id = id;
	}
	
	public int getPrisonerCounterId(){
		return id;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

//测试类
public class Test {
	
	final int PRISONER_COUNT = 100; //犯罪总人数
	
	Prisoner[] prisonArray = new Prisoner[PRISONER_COUNT];//犯罪人容器
	
	Prisoner prisonCounter;
	
	/**
	 * 初始化 
	 */
	public void init(){
		initPrison();
		PointPrisonCount();
	}
	
	/**
	 *	初始化罪犯 
	 */
	public void initPrison(){
		for(int i = 0; i < PRISONER_COUNT; i++){
			Prisoner prison = new Prisoner(i);
			prisonArray[i] = prison;
		}
	}
	
	/**
	 * 随机指定计数员
	 */
	public void PointPrisonCount(){
		RandomPointPrisonCount rppc = new RandomPointPrisonCount(prisonArray);//随机产生一个计数员
		Prisoner prison = new PrisonCounter(rppc.id, new Counter());
		prisonArray[rppc.id] = prison;
		prisonCounter = prison;
		System.out.println("当前计数员为:" + rppc.id);
	}
	
	public void runGame(){
		Light light = new Light();
		int day = 0;
		PrisonCounter counter = (PrisonCounter)prisonCounter;
		while(counter.getCount() < PRISONER_COUNT -1){
			Random rm = new Random();
			int id = rm.nextInt(PRISONER_COUNT);
			if(id == prisonCounter.id){//如果是计数员出来,灯亮就关闭
				if(light.isLight){
					counter.turnOff(light);
					counter.addCount();
				}
			}else{
				if(!light.isLight){
					if(!prisonArray[id].state){
						prisonArray[id].turnOn(light);
						prisonArray[id].state = true;
					}
				}
			}
			
			day++;
		}
		System.out.println("一共花了" + day + "天, 人员全部出狱!");
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Test test = new Test();
		test.init();
		test.runGame();
	}

}




老太太的裹脚布
0 请登录后投票
   发表时间:2010-01-15  
hkliya 写道
中发白 写道
就没人看看我的代码, 有人仔细分析了一下没

有点疑惑为什么属性都不加权限修饰符呢?


已添加。
0 请登录后投票
   发表时间:2010-01-15  
中发白 写道
hkliya 写道
中发白 写道
就没人看看我的代码, 有人仔细分析了一下没

有点疑惑为什么属性都不加权限修饰符呢?


已添加。


我觉得计数器类有点多余啊,你觉得呢
//计数器   
public class Counter {   
       
    private int count = 0;   
  
    public void addCount() {   
        count++;   
    }   
  
    public int getCount() {   
        return this.count;   
    }   
  
} 
0 请登录后投票
   发表时间:2010-01-15  
l101y1982j 写道
小问一句,大三那位
能否将for(;;)改为while(true)呢?

习惯
0 请登录后投票
   发表时间:2010-01-15  
红楼一梦 写道
l101y1982j 写道
小问一句,大三那位
能否将for(;;)改为while(true)呢?

习惯

在这时,for和whlie没什么区别,但平时还是用for好些
因为for的初始值,判断条件和步长都在一行,而whlie的步长一般写在最后一行,如果这个循环太长的话,就要拖上拖下,可读性就不如for了
所以可能for用习惯了,遇到现在这种情况还是写for(;;)了
0 请登录后投票
   发表时间:2010-01-15  
hkliya 写道

light=false; // 等号两边加空格哦

private HashMap<Integer,Prisoner> Prison; //变量名首字母小写哦

if(!once&&!light){   // 两个否定很晕哦

for(int i=1;i<100;i++)   //100这个Magic Number是什么意思哦

 

我从c++过来的,留着很多c++的习惯。等号两边加不加空格和变量小不小写这样的细节还的确没注意过。以前写c++从来不管这些。两个否定嘛,也是从c++那边搬来的习惯。我觉得c++的代码简洁,Java代码不如c++灵活。100这个数字,楼主要是不明白那我就没办法了,100个囚犯啊。

0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics