全WA但是感觉应该没错
查看原帖
全WA但是感觉应该没错
1510942
yuki_yuki_楼主2024/11/19 22:07
#include <bits/stdc++.h>
using namespace std;

bool isPrime(int num)
{
    // 小于2的数不是质数
    if (num < 2)
    {
        return false;
    }
    // 2是最小的质数
    if (num == 2)
    {
        return true;
    }
    // 排除所有偶数
    if (num % 2 == 0)
    {
        return false;
    }
    // 只需检查到 sqrt(num) 即可
    int root = static_cast<int>(sqrt(num));
    for (int i = 3; i <= root; i += 2)
    {
        if (num % i == 0)
        {
            return false; // 如果能被整除,则不是质数
        }
    }
    return true; // 以上都不满足,则是质数
}

int main()
{
    string s;
    cin >> s;
    vector <int> AlphaCnt(26,0);
    for (const auto &ch : s)
    {
        AlphaCnt[static_cast<int>(ch - 'a')]++;
    }
    sort(AlphaCnt.begin(), AlphaCnt.end());
    
    auto min = find_if(AlphaCnt.begin(), AlphaCnt.end(), [](int num) {
        return num != 0;
    }); 
    if (*min == s.size())
        *min = 0;
    //cout << *min<<endl;

    int deference = *(AlphaCnt.rbegin()) - *min;

    //cout <<*(AlphaCnt.rbegin())<<" "<<*min<<endl;
    //cout <<deference<<endl;

    if (isPrime(deference))
    {
        cout << "Lucky Word" << endl
             << deference;
    }
    else
    {
        cout << "No Answer" << endl
             << "0";
    }
}
2024/11/19 22:07
加载中...