猴子排序的期望

链接:https://ac.nowcoder.com/acm/problem/15825
来源:牛客网

题目描述
我们知道有一种神奇的排序方法叫做猴子排序,就是把待排序的数字写在卡片上,然后让猴子把卡片扔在空中,等落下的时候观察这些卡片是否从左到右已经排序完成(我们认为不会发生卡片落地后叠在一起的情况)如果有序则排序完成,否则让猴子再扔一遍,直到卡片有序,那么问题来了,给你N个卡片,每个卡片上写着一个大写字母,请问猴子第一次扔这些卡片就按字典序排序完成的概率有多大?

输入描述:
第一行是一个整数N(1<N<100)表示给猴子N张卡片,接下来是一个长度为N的字符串,代表这些卡片上所写的字母。
输出描述:
输出一行,表示猴子排序第一次就成功的概率(用分子为1的分数表示)。
示例1
输入
复制
7
SCIENCE
输出
复制
1/1260

思路:题意理解错误,最开始解出的是字符串的每个字符出现次数之积除于字符串长度的阶乘,但是后来才知道是字符串的每个字符出现次数阶乘的积除于字符串长度的阶乘

错误代码:

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        BigInteger n=sc.nextBigInteger();
        BigInteger ans=new BigInteger("1");
        BigInteger tmp=new BigInteger("0");
        BigInteger cnt=new BigInteger("1");
        while(n.compareTo(tmp)>0) {
            ans=ans.multiply(n);
            n=n.subtract(cnt);
        }
        String str=sc.next();
        BigInteger []num= {tmp,tmp,tmp,tmp,tmp,tmp,tmp,tmp,tmp,tmp,tmp,
                                    tmp,tmp,tmp,tmp,tmp,tmp,tmp,tmp,tmp,tmp,tmp,
                                    tmp,tmp,tmp,tmp};
        for(int i=0;i<str.length();i++){
            int temp=str.charAt(i)-'0'-17;
            num[temp]=num[temp].add(cnt);
        }
        BigInteger stp=new BigInteger("1");
        for(int i=0;i<num.length;i++) {
            if(num[i].compareTo(tmp)==0)
                continue;
            else
                stp=stp.multiply(num[i]);
        }
        BigInteger a=ans;
        BigInteger b=stp;
        BigInteger ind=tmp;
        while(b.compareTo(tmp)!=0){
            ind=a.remainder(b);
            a=b;
            b=ind;
        }
        stp=stp.divide(a);
        ans=ans.divide(a);
        System.out.println(stp+"/"+ans);
        sc.close();
    }
}

AC代码:

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        BigInteger n=sc.nextBigInteger();
        BigInteger ans=new BigInteger("1");
        BigInteger tmp=new BigInteger("0");
        BigInteger cnt=new BigInteger("1");
        while(n.compareTo(tmp)>0) {
            ans=ans.multiply(n);
            n=n.subtract(cnt);
        }
        String str=sc.next();
        BigInteger []num= {tmp,tmp,tmp,tmp,tmp,tmp,tmp,tmp,tmp,tmp,tmp,
        					tmp,tmp,tmp,tmp,tmp,tmp,tmp,tmp,tmp,tmp,tmp,
        					 tmp,tmp,tmp,tmp};
        for(int i=0;i<str.length();i++){
            int temp=str.charAt(i)-'0'-17;
            num[temp]=num[temp].add(cnt);
        }
        BigInteger stp=new BigInteger("1");
        for(int i=0;i<num.length;i++) {
        	if(num[i].compareTo(tmp)==0)
        		continue;
        	else{
                BigInteger nums=new BigInteger("1");
                while(num[i].compareTo(tmp)!=0){
                    nums=nums.multiply(num[i]);
                    num[i]=num[i].subtract(cnt);
                }
                stp=stp.multiply(nums);
            }
        		
        }
        BigInteger a=ans;
        BigInteger b=stp;
        BigInteger ind=tmp;
        while(b.compareTo(tmp)!=0){
        	ind=a.remainder(b);
        	a=b;
        	b=ind;
        }
        stp=stp.divide(a);
        ans=ans.divide(a);
        System.out.println(stp+"/"+ans);
        sc.close();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章