2011阿里巴巴程序设计公开赛 / 1009 Board Game Dice

这题一看就是算概率,把题意弄懂后,顺利推出公式,x*1/(n/mx) = mx*x/n.

因为精度wa了一次= =,然后输出少了空格PE了一次,额...


代码:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define int64 long long
int64 n, m, x;
#define INF 21623
int64 gcd(int64 a, int64 b)
{
	return b==0? a: gcd(b, a%b);
}

int64 getx(int64& mx)
{
	int64 ans = 1;
	int64 tn = n;	int64 tm = m;
	while(tm<tn)
	{
//printf("%d*%d=%d\n", tm, tm*tm);
		if((int64)tm*tm>tn)
		{
			tm*=m;
			ans+=1;
		}
		else if((int64)tm*tm<=tn)
		{
			tm*=tm;
			ans*=2;
		}
	}
	mx = tm;
	return ans;
}

void solve(int64& a, int64& b)
{
	int64 mx;
	x = getx(mx);
	int64 d = gcd((int64)mx*x, (int64)n);

	a = (int64)mx*x/d;
	b = n/d;
}

int main()
{
	int t;	cin>>t;
	for(int i=1; i<=t; i++)
	{
		cin>>n>>m;
		int64 a, b;
		solve(a,  b);
		cout<<"Case "<<i<<": "<<a<<"/"<<b<<endl;
	}
}

  • 0
    点赞
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值