1. 程式人生 > >墨燦網路筆試題

墨燦網路筆試題

剛去做了墨燦網路的筆試題,最後一道題由於時間的原因沒有寫完,現在回來補全。

體的大致意思是這樣的:現在有一副撲克牌(54)張,兩個人從中每人抽取兩張

撲克牌預設大小順序:大王>小王>A>K>Q>J>10>9>8>7>6>5>4>3>2

撲克牌組合:大王和小王最大

                    兩個牌點數相同大於兩個牌點數不同(比如2,2大於A,大王)

                    若兩個人兩個手牌都是對子(比如AA與QQ),則按預設大小比較

                    若兩個人兩個手牌皆不相同(則一對一按預設大小順序比較)

差不多就是這個意思,但是我感覺平手的局面介紹的不是很細緻(不是我沒記住,是這題邏輯不太嚴謹)

A:設計一個類OnePaper,表示單張牌的屬性;設計一個類HandsPaper表示每個人手裡牌的屬性;

B:設計一個方法計算誰獲勝;

解題:

public class HandsPaper {
	private OnePaper firstPaper;
	private OnePaper secondPaper;
	public OnePaper getSecondPaper() {
		return secondPaper;
	}
	public void setSecondPaper(OnePaper secondPaper) {
		this.secondPaper = secondPaper;
	}
	public OnePaper getFirstPaper() {
		return firstPaper;
	}
	public void setFirstPaper(OnePaper firstPaper) {
		this.firstPaper = firstPaper;
	}
	
}
public class OnePaper {
	private String pointPaper;

	public String getPointPaper() {
		return pointPaper;
	}

	public void setPointPaper(String pointPaper) {
		this.pointPaper = pointPaper;
	}
	
}
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import htc.puke.bucket.HandsPaper;
import htc.puke.bucket.OnePaper;

public class Main {
	private static Map<String,Integer> map = new HashMap<String,Integer>();
	public static void compare(HandsPaper h1,HandsPaper h2){
		String h1leftpaper = h1.getFirstPaper().getPointPaper();
		String h1rightpaper = h1.getSecondPaper().getPointPaper();
		String h2leftpaper = h2.getFirstPaper().getPointPaper();
		String h2rightpaper = h2.getSecondPaper().getPointPaper();
		if("大王".equals(h1leftpaper)&&"小王".equals(h1rightpaper)){
			System.out.println("h1贏了");
			return;
		}else if("大王".equals(h2leftpaper)&&"小王".equals(h2rightpaper)){
			System.out.println("h2贏了");
			return;
			//h1兩張牌相同
		}else if(h1leftpaper.equals(h1rightpaper)){
			//且h2兩張牌相同
			if(h2leftpaper.equals(h2rightpaper)){
				if(map.get(h1leftpaper)>map.get(h2leftpaper)){
					System.out.println("h1贏了");
				}else if(map.get(h1rightpaper)==map.get(h2leftpaper)){
					System.out.println("打成平手");
				}else{
					System.out.println("h2贏了");
				}
			}else{
				System.out.println("h1贏了");
			}
		}else if(h2leftpaper.equals(h2rightpaper)){
			System.out.println("h2贏了");
		}else{
			if(map.get(h1leftpaper)>map.get(h2leftpaper)){
				if(map.get(h1rightpaper)>=map.get(h2rightpaper)){
					System.out.println("h1贏了");
				}else{
					System.out.println("打成平手");
				}
			}else if(map.get(h1leftpaper)==map.get(h2leftpaper)){
				if(map.get(h1rightpaper)<map.get(h2rightpaper)){
					System.out.println("h2贏了");
				}else{
					System.out.println("打成平手");
				}
			}else{
				if(map.get(h1rightpaper)<=map.get(h2rightpaper)){
					System.out.println("h2贏了");
				}else{
					System.out.println("打成平手");
				}
			}
		}
		
	}
	public static void init(){
		map.put("大王", 15);
		map.put("小王",14);
		map.put("A", 13);
		map.put("K",12);
		map.put("Q",11);
		map.put("J",10);
		map.put("10",9);
		map.put("9",8);
		map.put("8",7);
		map.put("7",6);
		map.put("6",5);
		map.put("5",4);
		map.put("4",3);
		map.put("3",2);
		map.put("2",1);
	}
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		HandsPaper h1 = new HandsPaper();
		OnePaper ho1 = new OnePaper();
		OnePaper ho2 = new OnePaper();
		System.out.print("請輸入h1leftpaper:");
		ho1.setPointPaper(sc.nextLine());
		System.out.print("請輸入h1rightpaper:");
		ho2.setPointPaper(sc.nextLine());
		h1.setFirstPaper(ho1);
		h1.setSecondPaper(ho2);
		HandsPaper h2 = new HandsPaper();
		OnePaper hp1 = new OnePaper();
		OnePaper hp2 = new OnePaper();
		System.out.print("請輸入h2leftpaper:");
		hp1.setPointPaper(sc.nextLine());
		System.out.print("請輸入h2leftpaper:");
		hp2.setPointPaper(sc.nextLine());
		h2.setFirstPaper(hp1);
		h2.setSecondPaper(hp2);
		init();
		compare(h1,h2);
	}
}
覺得自己寫的不是很好,但是也算是完成了吧;