上机一 A The stupid owls

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);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值