interview test

来源:互联网 发布:世界征服者3 全将 数据 编辑:程序博客网 时间:2024/06/05 10:34
#include <iostream>#include <cstdio>using namespace std;typedef long long LL;long factorial(long number){   if(number<=1)        return 1;    else        return number*factorial(number-1);}int combinator(int n,int m){   int temp;    if(n>m)    {        return factorial(n)/(factorial(m)*factorial(n-m));    }    else if(n==m)    {        return  1;    }    else{        return 0;    }}double round(double number, unsigned int bits) {    LL integerPart = number;    number -= integerPart;    for (unsigned int i = 0; i < bits; ++i)        number *= 10;    number = (LL) (number + 0.5);    for (unsigned int i = 0; i < bits; ++i)        number /= 10;    return integerPart + number;}int main(){    int n;    int m;    cin>>n>>m;    cout<<combinator(n,1)<<endl;    cout<<combinator(m,1)<<endl;    float x=(float)(combinator(m,2)*2)/combinator(n+m,2)+(float)(combinator(n,1)*combinator(m,1))/combinator(n+m,2);    cout<<x<<endl;    float t=(float)m/x;    cout<<t<<endl;    printf("%.1f\n", round(t, 1));    return 0;}
原创粉丝点击