求助,不知错因的孩纸。。。

P1591 阶乘数码

MOXCOOT @ 2021-03-07 18:46:06

#include <iostream>
#include <vector>
#include <cstring>
#define NUM 1010
using namespace std;
int n;
int find(int* a, int x) {                          //找到a[]中x的个数
    int cnt = 0;
    bool flag = 0;
    for (int i = NUM - 1; i>=0; i--) {
        if (a[i])flag = 1;
        if (a[i] == x && flag == 1)cnt++;
    }
    return cnt;
}
void multi(int* a, int x) {                      //a[]*x函数
    int last = 0;
    for (int i = 0; i < NUM; i++) {
        a[i] = a[i] * x + last;
        last = a[i] / 10;
        a[i] %= 10;
    }
}
int nub(int x, int y) {                        
    int a[NUM] = { 0 };
    a[0] = 1;
    for (int i = 1; i <= x; i++) {
        multi(a, i);
    }
    return find(a, y);
}
int main() {
    cin >> n;
    int t[12];
    int tt = 0;
    for (int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;
        t[i] = nub(x, y);
        tt++;
    }
    for (int i = 0; i < tt; i++) {
        cout << t[i] << endl;
    }
}

by Heyf @ 2021-03-07 19:29:11

等我帮你调

试试这个

import math
for i in range(int(input())):
    n,a = input().split()
    print(str(math.factorial(int(n))).count(a))

by Heyf @ 2021-03-07 19:31:37

#include <iostream>
#include <vector>
#include <cstring>
#define NUM 101000  //数组开小了
using namespace std;
int n;
int find(int* a, int x) {                          //找到a[]中x的个数
    int cnt = 0;
    bool flag = 0;
    for (int i = NUM - 1; i>=0; i--) {
        if (a[i])flag = 1;
        if (a[i] == x && flag == 1)cnt++;
    }
    return cnt;
}
void multi(int* a, int x) {                      //a[]*x函数
    int last = 0;
    for (int i = 0; i < NUM; i++) {
        a[i] = a[i] * x + last;
        last = a[i] / 10;
        a[i] %= 10;
    }
}
int nub(int x, int y) {                        
    int a[NUM] = { 0 };
    a[0] = 1;
    for (int i = 1; i <= x; i++) {
        multi(a, i);
    }
    return find(a, y);
}
int main() {
    cin >> n;
    int t[12];
    int tt = 0;
    for (int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;
        t[i] = nub(x, y);
        tt++;
    }
    for (int i = 0; i < tt; i++) {
        cout << t[i] << endl;
    }
}

by Heyf @ 2021-03-07 19:32:07

@CYL920106840414 看注释可AC


by MOXCOOT @ 2021-03-07 19:39:22

@Heyf 好了,多谢dalao,


|