BUAA OJ 871 The stupid owls

BUAA OJ 871 The stupid owls

题目描述

Bamboo recently bought an owl for sending and receiving letters. Every owl will bring a letter to exactly one person, so normally Bamboo’s owl will only bring letters for her.

Although most owls sold in the magical stores are extremely conscientious almost all the time, sometimes they can also make mistakes. For example, on one occasion Bamboo’s owl brought a letter to her roommate Eve, and Eve’s owl brought a letter to Bamboo .The two stupid owls made mistakes at the same time!

What’s worse, if there were n people, each with a stupid owl, so that every one might get a wrong letter . So what the probability of no one getting his or her correct letter?

输入

The input file will contain a list of positive integers n, one per line(0<n<25)。

输出

For each integer n in the input, output the probability(in percentage form) of the n people all receiving a letter that ought to be sent to another one.

Print each output in one line. Please keep two decimals.

解题思路

数论

错排公式

D ( n ) = ( n − 1 ) [ D ( n − 2 ) + D ( n − 1 ) ] D(n) = (n-1) [D(n-2) + D(n-1)] D(n)=(n1)[D(n2)+D(n1)]
特殊地, D ( 1 ) = 0 , D ( 2 ) = 1 D(1) = 0, D(2) = 1 D(1)=0,D(2)=1.

错排的概率只需再除以总数 n ! n! n!

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>

using namespace std;
long long d[30];


int main() {
    int n;
    d[2] = 1;
    for (int i = 3; i < 25; ++i) {
        d[i] = (i - 1) * (d[i - 2] + d[i - 1]);
    }
    while (~scanf("%d", &n)) {
        long long all = 1;
        for (int i = n; i > 1; --i) {
            all *= i;
        }
        cout << setiosflags(ios::fixed) << setprecision(2) << 100 * (double) d[n] / all << '\%' << '\n';
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值