猴子排序的期望

鏈接: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();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章