南阳oj入门题-coin test

本文介绍了一个基于硬币投掷实验的概率计算问题,通过分析大量硬币正反面及站立出现的可能性,探讨了如何准确计算硬币正面朝上概率的方法,并提供了一段C++代码实现。
/**
Coin Test
时间限制:3000 ms  |  内存限制:65535 KB
难度:1
描述
As is known to all,if you throw a coin up and let it droped on the desk there are usually three results. Yes,just believe what I say ~it can be the right side or the other side or standing on the desk, If you don't believe this,just try In the past there were some famous mathematicians working on this .They repeat the throwing job once again. But jacmy is a lazy boy.He is busy with dating or playing games.He have no time to throw a single coin for 100000 times. Here comes his idea,He just go bank and exchange thousands of dollars into coins and then throw then on the desk only once. The only job left for him is to count the number of coins with three conditions.

He will show you the coins on the desk to you one by one. Please tell him the possiblility of the coin on the right side as a fractional number if the possiblity between the result and 0.5 is no larger than 0.003. BE CAREFUL that even 1/2,50/100,33/66 are equal only 1/2 is accepted ! if the difference between the result and 0.5 is larger than 0.003,Please tell him "Fail".Or if you see one coin standing on the desk,just say "Bingo" any way.

输入
Three will be two line as input.
The first line is a number N(1<N<65536)
telling you the number of coins on the desk.
The second line is the result with N litters.The letter are "U","D",or "S","U" means the coin is on the right side. "D" means the coin is on the other side ."S" means standing on the desk.
输出
If test successeded,just output the possibility of the coin on the right side.If the test failed please output "Fail",If there is one or more"S",please output "Bingo"
样例输入
6
UUUDDD
样例输出
1/2
*/
#include<iostream>
using namespace std;
//求最大公约数
int yueshu(int n,int m)
{
	if(n==0)
		return m;
	else
		return yueshu(m%n,n);	
} 
int main()
{
	int T;
	cin>>T;
	int u=0,d=0;
	while(T--)
	{
		char c;
		cin>>c;
		if(c=='S')
		{
			cout<<"bingo"<<endl;
			break;	
		}		
		else if(c=='U')
			u++;
		else if(c=='D')
			d++;
	}
	int g=yueshu(u,u+d);
	if((u/(u+d))-1/2>0.003||(u/(u+d))-1/2<-0.003)
		cout<<"Fail"<<endl;
	else
		cout<<u/g<<"/"<<(u+d)/g<<endl;
}

 

### 关于SWUST OJ上的机器人收集硬币问的解决方案 在解决SWUST OJ上的“机器人收集硬币”问时,需要考虑动态规划和路径优化的相关算法。这类问通常可以归类为二维平面上的路径规划问,其中目标是找到一条从起点到终点的路径,使得收集的硬币数量最大化[^1]。 #### 动态规划方法 动态规划是一种有效的解决此类问的方法。通过定义状态`dp[i][j]`表示到达位置`(i, j)`时能够收集到的最大硬币数,可以递推地计算出最终解。转移方程如下: ```python if grid[i][j] == 1: # 如果当前位置有硬币 dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + 1 else: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) ``` 其中,`grid[i][j]`表示位置`(i, j)`是否有硬币,`dp[i][j]`表示到达该位置时最多能收集的硬币数。边界条件需要特别注意,例如当`i=0`或`j=0`时,只能从上方或左方移动过来[^2]。 #### 贪心算法的局限性 尽管贪心算法可能在某些简单情况下有效,但对于更复杂的地图布局,它往往无法保证全局最优解。这是因为贪心算法只关注局部最优解,而动态规划则通过递推关系确保了全局最优解的计算[^3]。 #### 示例代码 以下是一个简单的Python实现示例,用于求解机器人收集硬币问: ```python def max_coins(grid): if not grid or not grid[0]: return 0 m, n = len(grid), len(grid[0]) dp = [[0]*n for _ in range(m)] for i in range(m): for j in range(n): if grid[i][j] == 1: # 当前位置有硬币 if i > 0 and j > 0: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + 1 elif i > 0: dp[i][j] = dp[i-1][j] + 1 elif j > 0: dp[i][j] = dp[i][j-1] + 1 else: dp[i][j] = 1 else: if i > 0 and j > 0: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) elif i > 0: dp[i][j] = dp[i-1][j] elif j > 0: dp[i][j] = dp[i][j-1] return dp[m-1][n-1] # 示例输入 grid = [ [1, 0, 1, 0], [1, 1, 0, 1], [0, 1, 1, 1] ] print(max_coins(grid)) # 输出最大硬币数 ``` 上述代码中,`grid`是一个二维数组,表示地图上每个位置是否有硬币。通过动态规划计算出从左上角到右下角的最大硬币数。 ### 注意事项 在实际实现过程中,还需要注意输入数据的边界条件以及性能优化。如果地图规模较大,可以考虑使用滚动数组来减少空间复杂度[^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

mind_programmonkey

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

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

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

打赏作者

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

抵扣说明:

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

余额充值