上机一 A The stupid owls

来源:互联网 发布:淘宝充值软件赚钱吗 编辑:程序博客网 时间:2024/06/02 00:50

A The stupid owls

时间限制:1000ms   内存限制:65536kb

通过率:159/172 (92.44%)    正确率:159/449 (35.41%)

题目描述

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.

输入样例

2

输出样例

50.00%

解析:

本质是错排问题。本题求错排概率,即错排数/全排列数,控制精度输出即可。

错排公式:f(1) = 0, f(2) = 1,f(n) = (n-1)[f(n-2)+f(n-1)] (n>2)。

错排解析:算法——错排问题

代码

#include<cstdio>long long dis_arg(int);long long total_arg(int);int main(){    int n;    while(~scanf("%d",&n))    {        double ans = 100 * (double)dis_arg(n)/(double)total_arg(n);        printf("%.2lf",ans);        puts("%");    }}long long dis_arg(int n){    if(n == 1)        return 0;    if(n == 2)        return 1;    return (n-1)*(dis_arg(n-1)+dis_arg(n-2));}long long total_arg(int n){    if(n == 1)    {        return 1;    }    return n*total_arg(n-1);}


原创粉丝点击