Gym 100814A---Arcade Game

本文介绍了一个基于数字排列组合的游戏,玩家需要计算赢得游戏的概率。通过输入一个整数,游戏会随机重新排列该数字的位数,如果新形成的数字大于原数字且达到最大可能的排列,则玩家获胜。文章提供了一个算法实现,用于计算不同输入下获胜的概率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description
standard input/output
Statements
Arcade mall is a new modern mall. It has a new hammer game called “Arcade Game”. In this game you’re presented with a number n which is hanged on a wall on top of a long vertical tube, at the bottom of the tube there is a button that you should hit with your hammer.

When you hit the button with all your force (as you always do), a ball is pushed all over the tube and hit the number n. The number n flies in the air and it’s digits fall back in any random permutation with uniform probability.

If the new number formed is less than or equal to the previous number, the game ends and you lose what ever the new number is. Otherwise (if the number is greater than the previous number), you are still in the game and you should hit the button again.

You win if the new number formed is greater than the previous number and it is equal to the greatest possible permutation number.

Can you compute the probability of winning?

Input
The first line of the input contains the number of test cases T. Following that there are T lines represents T test cases. In each line, there is a single integer (1 ≤ n ≤ 109) the target number. The digits of n are all unique, which means that any 2 digits of n are different.

Output
For each test case, print one line containing the answer. Print the answer rounded to exactly 9 decimal digits.

Sample Input
Input
3
952
925
592
Output
0.000000000
0.166666667
0.194444444
Hint
In the first test case, the answer is 0 because 952 is greater than all 2,5 and 9 permutations so you can’t win, whatever you do.

In the second test case, the answer is 0.166666667 because you may win by getting number 952 with probability 1/6.

In the third test case the answer is 0.194444444 because you may win by getting number 952 in round1 with probability 1/6 or you can win by getting number 925 in round 1 and then 952 in round 2 with probability 1/6 * 1/6.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int n[15]={1,1};
int a[15]; 
double p,q;
int main()
{
    for(int i=2;i<10;i++)
    {
        n[i]=n[i-1]*i;
    }
    int T;
    cin>>T;
    while(T--)
    {
        char s[11];
        int cnt=0;
        scanf("%s",s);
        int l=strlen(s);
        for(int i=0;i<l;i++)
        {
            a[i]=s[i]-'0';
        }
        while(next_permutation(a,a+l))
        {
            cnt++;
        }
        if(cnt==0)
        {
            printf("0.000000000\n");
        }
        else
        {
            double temp=1.0/n[l];
            q=p=temp;
            for(int i=1;i<cnt;i++)
            {
                q=p+p*temp;
                p=q;
            }
            printf("%.9lf\n",p);
        }       
    }
    return 0;
}

仅代表个人观点,欢迎交流探讨,勿喷~

这里写图片描述

PhotoBy:WLOP

http://weibo.com/wlop

上面的代码运行提示下面的错误025-03-05 16:41:41.184542: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`. 2025-03-05 16:41:42.563063: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`. A.L.E: Arcade Learning Environment (version 0.8.1+53f58b7) [Powered by Stella] Traceback (most recent call last): File "d:\py\demo\bokeh_demo.py", line 10, in <module> env = gym.make("BreakoutNoFrameskip-v4") File "d:\python39\lib\site-packages\gym\envs\registration.py", line 652, in make raise e File "d:\python39\lib\site-packages\gym\envs\registration.py", line 640, in make env = env_creator(**_kwargs) File "d:\python39\lib\site-packages\ale_py\env\gym.py", line 155, in __init__ self.seed() File "d:\python39\lib\site-packages\ale_py\env\gym.py", line 205, in seed if not hasattr(roms, self._game): File "d:\python39\lib\site-packages\ale_py\roms\__init__.py", line 118, in __getattr__ path = _resolve_rom(name) File "d:\python39\lib\site-packages\ale_py\roms\__init__.py", line 48, in _resolve_rom rom_path = package.resolve(rom_id) File "d:\python39\lib\site-packages\ale_py\roms\plugins.py", line 39, in resolve rom = resources.files(self.package).joinpath(f"{id}.bin") File "d:\python39\lib\importlib\resources.py", line 147, in files return _common.from_package(_get_package(package)) File "d:\python39\lib\importlib\_common.py", line 14, in from_package return fallback_resources(package.__spec__) File "d:\python39\lib\importlib\_common.py", line 18, in fallback_resources package_directory = pathlib.Path(spec.origin).parent File "d:\python39\lib\pathlib.py", line 1082, in __new__ self = cls._from_parts(args, init=False) File "d:\python39\lib\pathlib.py", line 707, in _from_parts drv, root, parts = self._parse_args(args) File "d:\python39\lib\pathlib.py", line 691, in _parse_args a = os.fspath(a) TypeError: expected str, bytes or os.PathLike object, not NoneType
最新发布
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值