打牌遊戲

CSDN 打牌活動,只能大學生參加。貼上代碼望糾正。


一群人想打斗地主和升级。已知他们已经1个、2个、3个或者4个人成为一桌了,现在要把他们全部变成3人一桌或者4人一桌,问至少移动几个人?(要保证所有人都能进行游戏) 输入格式: 多组数据,每组数据一行,包含4个整数a,b,c,d 0<=a,b,c,d<=100000000表示1个人、两个人、3个人、4个人的桌数。 输出格式: 至少移动的人数,如果不可能完成,输出-1。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PokerGame {

	class Tables{
		private int oneTable;
		private int twoTable;
		private int thrTable;
		private int fouTable;

		public void setOneTable(int oneTable) {
			this.oneTable = oneTable;
		}

		public int getTwoTable() {
			return twoTable;
		}

		public void setTwoTable(int twoTable) {
			this.twoTable = twoTable;
		}

		public int getThrTable() {
			return thrTable;
		}

		public void setThrTable(int thrTable) {
			this.thrTable = thrTable;
		}

		public int getFouTable() {
			return fouTable;
		}

		public void setFouTable(int fouTable) {
			this.fouTable = fouTable;
		}

		public int getOneTable() {
			return oneTable;
		}

		public int getTotal(){
			return oneTable * 1 + twoTable * 2 + thrTable * 3 + fouTable * 4;
		}
		
	}
	
	public static void main(String[] args) {
		new PokerGame().start();
	}

	public void start(){
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		String in  = null;
		Tables table;
		try {
			while((in = reader.readLine()) != null){
				table = new Tables();
				convert(in.split(" "), table);
				int person = work(table);
				switch(person){
					case 0 : System.out.println("No need to remove."); break;
					case -1: System.out.println("Can't remove to agree.");break;
					default: System.out.println("Need to move person: " + person);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void convert(String[] data, Tables table){
		table.setOneTable(Integer.parseInt(data[0]));
		table.setTwoTable(Integer.parseInt(data[1]));
		table.setThrTable(Integer.parseInt(data[2]));
		table.setFouTable(Integer.parseInt(data[3]));
	}

	public int work(Tables table) {
		int total = table.getTotal();
		boolean thrTable = total % 3 == 0;
		boolean fouTable = total % 4 == 0;
		if(!thrTable && !fouTable) return -1;
		
		if(thrTable && fouTable){
			return Math.min(getThreeMax(table), getFouMax(table));
		} else if(thrTable) {
			return getThreeMax(table);
		} else {
			return getFouMax(table);
		}
	}

	public int getThreeMax(Tables table){
		int more = table.getFouTable();
		int lessOne = table.getOneTable() * 2;
		int lessTwo = table.getTwoTable() * 1;
		if(table.getTwoTable() > 0){
			if(lessTwo == more + lessOne){
				return lessTwo;
			} else{
				int stillNeed = lessTwo - table.getOneTable();
				return stillNeed > 0 ? (stillNeed + ((more - stillNeed) * 2) / 3) : (((table.getOneTable() - lessTwo + more) * 2) /3);
			}
		} else {
			return table.getFouTable() + table.getOneTable();
		}
	}

	public int getFouMax(Tables table){
		int lessThree = table.getThrTable();
		int stillNeed = lessThree - table.getOneTable();
		if(stillNeed > 0){
			if(stillNeed % 2 == 0){
				return stillNeed + (table.getTwoTable() - stillNeed / 2) / 2;
			} 
			return -1;
		} else{
			int more = -stillNeed;
			return (table.getTwoTable() / 2) + more / 2;
		}
	}
	public int getTotal(int[] data){
		return data[0] * 1 + data[1] * 2 + data[2] * 3 + data[3] * 4;
	}
}
望糾正!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值