其他分享
首页 > 其他分享> > 蓝桥杯习题——印章

蓝桥杯习题——印章

作者:互联网

印章

#include <bits/stdc++.h>
//#pragma GCC optimize(3, "Ofast", "inline")//03优化有问题我nm
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define lowbit(x) x &(-x)
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
const int N = 1e5 + 100; //sqrt(1e9)
const int M = 1e6 + 100;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
//const int eps = 1e-8;
const double pi = acos(-1.0); //pi=3.1415926...
int n, m;
double dp[100][100], p;

void init()
{
    for (int i = 1; i <= m; i++)//
    {
//        dp[i][1] = (i == 1 ? 1 : pow(1.0 / n, i - 1));
        for (int j = 1; j <= n; j++)
        {
            if (j == 1)
            {
                dp[i][j] = pow(p, i - 1);
//                puts("dwadad");
            } else if (i < j)
            {
                dp[i][j] = 0;
//                puts("*********");
            }//
            else
            {
                dp[i][j] = dp[i - 1][j] * (j * 1.0 / n) + dp[i - 1][j - 1] * ((n - j + 1) * 1.0 / n);
//                printf("%d**********%d\n", i, j);
            }
        }
    }
}

int main()
{
    //    IOS;
    scanf("%d%d", &n, &m);
    p = 1.0 / n;
//    cout << p << endl;
//    cout << pow(p, 1 - 1) << endl;
    init();
    printf("%.4lf\n", dp[m][n]);
}

就一个dp,没啥好说的;总结一下今天发现的一个坑,else 只对上面的那个if【或者else if】有用,如果是if,if,else 这样的话,程序会先进入第一个if ,不满足第二个if 时候进入else ,这样导致可能会覆盖第一个if 的结果
解决办法就是可以换成if,else if,else这样

标签:const,int,else,蓝桥,印章,1.0,习题,dp,define
来源: https://blog.csdn.net/qq_51201910/article/details/121468629