某找房網 筆試題(二)

一、題目說明:

傻傻的搏鬥

小智和小春兩個遊戲菜鳥要進行電競搏鬥,小智有X點HP,每次攻擊便使對方丟失A點生命值,每次攻擊完後需要冷卻C秒,小春有Y點HP,每次攻擊會使對方丟失B點生命值,每次攻擊完後需要冷卻D秒。玩家HP小於等於0時便死亡,若小智最終存活,則輸出XIAOZHI,若小春最終存活,則輸出XIAOCHUN。若兩者一起死亡,則輸出TIE。

二、樣例輸入 

4

1 2 3 4 5 6

128 39 20 109 100 92

1000 10 39 33 333 39

101 10 10 101 20 20

三、樣例輸出 

XIAOCHUN

XIAOZHI

TIE

TIE

 四、代碼實現(java語言)

package test;
/**
 * 傻傻的搏鬥 解答
 */
import java.lang.reflect.Array;
import java.util.Scanner;

public class ZhaoFangT2 {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int lines = scan.nextInt();
		scan.nextLine();
		int count = 0;
		String eachLine = "";
		int X,A,C,Y,B,D;
		String[] resultStr = new String[lines];
		while(count<lines){
			eachLine = scan.nextLine();
			String[] eachStr = eachLine.split(" ");
			X = Integer.parseInt(eachStr[0]); //小智HP
			A = Integer.parseInt(eachStr[1]); //小智使對方丟失點數
			C = Integer.parseInt(eachStr[2]); //小智冷卻時間
			Y = Integer.parseInt(eachStr[3]); //小春HP
			B = Integer.parseInt(eachStr[4]); //小春使對方丟失點數
			D = Integer.parseInt(eachStr[5]); //小春冷卻時間
			String eachresult = getResult(X,A,C,Y,B,D);
			resultStr[count] = eachresult;
			count++;
		}
		
		for(int i=0; i<lines; i++){
			System.out.println(resultStr[i]);
		}
	}

	private static String getResult(int x, int a, int c, int y, int b, int d) {
		//初始判斷
		if(x<=0 && y<=0){
			return "TIE";
		}else if(x<=0){
			return "XIAOCHUN";
		}else if(y<=0){
			return "XIAOZHI";
		}
		//開始攻擊
		int zhiCount = (int) (Math.ceil((double)y/a)-1); //小智需要攻擊次數
		int zhiTime = zhiCount*c; //小智需要冷卻的時間
		
		int chunCount = (int) (Math.ceil((double)x/b)-1);//小春需要攻擊的次數		
		int chunTime = chunCount*d; //小春需要冷卻的時間
		
		if(zhiTime == chunTime){
			return "TIE";
		}else if(zhiTime > chunTime){
			return "XIAOCHUN";
		}else{
			return "XIAOZHI";
		}
		
	}
}

六、說明:

該代碼並沒有進行完善的測試,如果有什麼問題,歡迎大家批評指正!共同進步! 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章