求debug,dalao们,代码应该很容易看懂,但不知道哪里错了
查看原帖
求debug,dalao们,代码应该很容易看懂,但不知道哪里错了
1254052
yangyang003楼主2024/10/30 12:26
#include <iostream>
#include <cmath>
using namespace std;
int t, ans;
string s;
int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool isprime(int n)
{
    if (n <= 1)
        return false; // 1 及以下的数不是质数
    if (n <= 3)
        return true; // 2 和 3 是质数
    if (n % 2 == 0 || n % 3 == 0)
        return false; // 2 和 3 的倍数不是质数
    for (int i = 5; i * i <= n; i += 6)
    {
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
    }
    return true;
}
bool check()
{
    int year = (s[0] - '0') * 1000 + (s[1] - '0') * 100 + 
    (s[2] - '0') * 10 + (s[3] - '0'), month = (s[4] - '0') * 10
    + (s[5] - '0'), day = (s[6] - '0') * 10 + (s[7] - '0');
    
    if (!isprime(year)) return false;
    
    int t = 0;
    if ((year % 100 && year % 4 == 0) || year % 400 == 0) t = 1;
    if (!t && month == 2 && day == 29) return false;
    if (month < 1 || month > 12 || day > m[month]) return false;
    if (!isprime(month)) return false;
    
    if (!isprime(day)) return false;
    int x = 10000000, sum = 0;
    for (int i = 0; i < s.size(); ++ i )
    {
        sum += (s[i] - '0') * x;
        x /= 10;
    }
    if (!isprime(sum)) return false;
    return true;
}
void dfs()
{
    int t = 1;
    for (int i = 0; i < s.size(); ++ i )
    if (s[i] == '-')
    {
        t = 0;
        break;
    }
    if (t && check()) ans ++;
    for (int i = 0; i < s.size(); ++ i )
    {
        if (s[i] == '-')
        {
            for (int j = 0; j <= 9; ++ j )
            s[i] = j + '0';
            dfs();
            s[i] = '-';
        }
    }
}
int main()
{
    cin >> t;
    while (t -- )
    {
        ans = 0;
        cin >> s;
        dfs();
        cout << ans << endl;
    }
    return 0;
}
2024/10/30 12:26
加载中...