WA,0分

P1591 阶乘数码

lzy20091001 @ 2023-06-05 12:44:41

测评信息

照着高精模板写的,WA,0分


by lzy20091001 @ 2023-06-05 12:45:16

#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;

string add(string, string);
string mul(string, int);

int main()
{
std:
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t;
    int n, maxn = 0;
    char a;
    string fac[1001] = {"1"};
    cin >> t;
    for (int i = 1; i <= t; i++)
    {
        cin >> n >> a;
        if (n <= maxn)
            cout << count(fac[n].begin(), fac[n].end(), a) << "\n";
        else
        {
            for (int j = maxn + 1; j <= n; j++)
                fac[j] = mul(fac[j - 1], j);
            maxn = n;
            cout << count(fac[n].begin(), fac[n].end(), a) << "\n";
        }
    }
    return 0;
}

string add(string str1, string str2)
{
    string str = "";
    int str1_len = str1.length(), str2_len = str2.length(), temp = 0;
    bool carry = 0;
    if (str1_len < str2_len)
        for (int i = 1; i <= str2_len - str1_len; i++)
            str1 = '0' + str1;
    else
        for (int i = 1; i <= str1_len - str2_len; i++)
            str2 = '0' + str2;
    str1_len = str1.length(), str2_len = str.length();
    for (int i = str1_len - 1; i >= 0; i--)
    {
        temp = str1[i] - '0' + str2[i] - '0' + carry;
        carry = temp / 10;
        temp %= 10;
        str = char(temp + '0') + str;
    }
    if (carry == 1)
        str = '1' + str;
    return str;
}

string mul(string a, int b)
{
    string str = "0";
    int a_len = a.length();
    for (int i = a_len - 1; i >= 0; i--)
    {
        string temp = "";
        temp = to_string((a[i] - '0') * int(pow(10, a_len - i - 1)) * b);
        str = add(str, temp);
    }
    return str;
}

by lzy20091001 @ 2023-06-05 13:16:36

@lzy20091001 改了一下,还是WA

#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;

string add(string, string);
string mul(string, int);

int main()
{
std:
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t;
    int n, maxn = 1;
    char a;
    string fac[1001] = {"0", "1"};
    cin >> t;
    for (int i = 1; i <= t; i++)
    {
        cin >> n >> a;
        if (n <= maxn)
            cout << count(fac[n].begin(), fac[n].end(), a) << "\n";
        else
        {
            for (int j = maxn + 1; j <= n; j++)
                fac[j] = mul(fac[j - 1], j);
            maxn = n;
            cout << count(fac[n].begin(), fac[n].end(), a) << "\n";
        }
    }
    return 0;
}

string add(string str1, string str2)
{
    string str = "";
    int str1_len = str1.length(), str2_len = str2.length(), temp = 0;
    bool carry = 0;
    if (str1_len < str2_len)
        for (int i = 1; i <= str2_len - str1_len; i++)
            str1 = '0' + str1;
    else
        for (int i = 1; i <= str1_len - str2_len; i++)
            str2 = '0' + str2;
    str1_len = str1.length(), str2_len = str.length();
    for (int i = str1_len - 1; i >= 0; i--)
    {
        temp = str1[i] - '0' + str2[i] - '0' + carry;
        carry = temp / 10;
        temp %= 10;
        str = char(temp + '0') + str;
    }
    if (carry == 1)
        str = '1' + str;
    return str;
}

string mul(string a, int b)
{
    string str = "0";
    int a_len = a.length();
    for (int i = a_len - 1; i >= 0; i--)
    {
        string temp = "";
        temp = to_string((a[i] - '0') * int(pow(10, a_len - i - 1)) * b);
        str = add(str, temp);
    }
    return str;
}

|