Greedy Gift Givers java

题目:

对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少。在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人。然而,在任何一群朋友中,有些人将送出较多的礼物(可能是因为有较多的朋友),有些人有准备了较多的钱。给出一群朋友,没有人的名字会长于 14 字符,给出每个人将花在送礼上的钱,和将收到他的礼物的人的列表,请确定每个人收到的比送出的钱多的数目。
 

输入格式:

第 1 行: 人数NP,2<= NP<=10 
第 2 行 到 第NP+1 行:这NP个在组里人的名字一个名字一行 
第NP+2到最后: 
这里的I段内容是这样组织的: 
第一行是将会送出礼物人的名字。 
第二行包含二个数字:第一个是原有的钱的数目(在0到2000的范围里),第二个 NGi 是将收到这个人礼物的人的个数 如果 NGi 是非零的, 在下面 NGi 行列出礼物的接受者的名字,一个名字一行。
 

样例: 
输入


dave 
laura 
owen 
vick 
amr 
dave 
200 3 
laura 
owen 
vick 
owen 
500 1 
dave 
amr 
150 2 
vick 
owen 
laura 
0 2 
amr 
vick 
vick 
0 0
 

输出

dave 302 
laura 66 
owen -359 
vick 141 
amr -150


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;


public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		
		int cnt = Integer.parseInt(in.readLine());
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		String[] names = new String[cnt];
		for (int i = 0; i < cnt; i++){
			names[i] = in.readLine();
			map.put(names[i], 0);
		}
		
		/**
		 * receiverCount 收到钱人数
		 * remainingMoney 剩下的钱
		 * receiverGetMoney 接受到的钱
		 * 
		 */
		for (int i = 0; i < cnt; i++){
			String name = in.readLine();
			StringTokenizer st = new StringTokenizer(in.readLine());
			int money = Integer.parseInt(st.nextToken());
			int receiverCount =  Integer.parseInt(st.nextToken());
			if (receiverCount == 0){
				continue;
			}
			
			int remainingMoney  = money % receiverCount;
			int receiverGetMoney = money / receiverCount;
			map.put(name, map.get(name) - (money - remainingMoney));
			
			for (int j = 0; j < receiverCount; j++){
				String receiver  = in.readLine();
				map.put(receiver, map.get(receiver) + receiverGetMoney);
			}
		}
		for (String s : names){
			System.out.println(s + " " + map.get(s));
		}
	}
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当然可以,以下是一个简单的Greedy Dice游戏java程序: ```java import java.util.*; public class GreedyDice { public static void main(String[] args) { Scanner input = new Scanner(System.in); int currentPlayer = 1; int[] scores = new int[2]; // 两名玩家的得分 int turnTotal = 0; boolean isGameOver = false; while (!isGameOver) { System.out.println("-------------- Round " + (currentPlayer == 1 ? "One" : "Two") + " --------------"); System.out.println("Current scores: Player One - " + scores[0] + ", Player Two - " + scores[1]); System.out.println("It's player " + currentPlayer + "'s turn."); String choice = ""; int roll = 0; while (choice != "h" && (roll == 0 || choice != "s")) { roll = rollDie(); System.out.println("You rolled a " + roll); if (roll == 1) { System.out.println("Uh oh, you lose all your points this turn."); turnTotal = 0; choice = "s"; } else { turnTotal += roll; System.out.println("Your turn total is now " + turnTotal); System.out.print("Do you want to (h)old or (r)oll? "); choice = input.nextLine(); } } if (choice.equals("s")) { // 玩家选择停止掷骰子 System.out.println("Turn over. You earned " + turnTotal + " points this turn."); scores[currentPlayer - 1] += turnTotal; turnTotal = 0; if (scores[currentPlayer - 1] >= 100) { // 判断是否获胜并结束游戏 isGameOver = true; System.out.println("Congratulations, player " + currentPlayer + "! You won the game!"); } else { currentPlayer = currentPlayer == 1 ? 2 : 1; // 切换到下一个玩家 } } else { // 玩家选择继续掷骰子 System.out.println("You chose to roll again. Good luck!"); } } } // 随机生成1~6的整数,模拟掷骰子 public static int rollDie() { return (int) (Math.random() * 6) + 1; } } ``` 该程序实现了简单的控制台界面,玩家可通过输入"h"或"s"来选择是否继续掷骰子,当其中一名玩家得分达到100分后,游戏结束,程序输出获胜者信息。 希望能帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dayaoK

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值