质数天(字符串年份的分解+质数判断)

我们将所有日期为质数的天数定义为质数天。 例如:2019年8月23日我们可以写成20190823,我们很容易发现这是一个质数。但我们的质数天同时还需要满足

20190823

0190823

190823

90823

0823

823

23

3

以上数均为质数,我们才称作质数天。我们想知道从21世纪 30世纪(2000年1月1日至2999年12月31日)有多少个质数天。

 AC CODE

#include <bits/stdc++.h>
using namespace std;

int f[8] = {1, 10000000, 1000000, 100000, 10000, 1000, 100, 10};
//不是闰年
int nr[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//是闰年
int yr[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int ans;

bool is_runyear(int y)
{
    return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}
bool is_prime(int x)
{    
    if (x == 0 || x == 1)
        return false;
    int tag = 1;
    int k = sqrt(x);
    for (int i = 2; i <= k; i++)
    {
        if (x % i == 0)
        {
            tag = 0;
            break;
        }
    }
    if (tag)
        return true;
    else
        return false;
}
void jc(int x)
{
    int tmp, cnt = 0;
    for (int i = 0; i < 8; i++)
    {
        if (i == 0)
            tmp = x;
        else
            tmp = x % f[i];
        if (is_prime(tmp))
            cnt++;
    }
    if (cnt == 8)
    {
        cout << x << " ";
        ans++;
        if (ans % 5 == 0)
            cout << endl;
    }
}
int main()
{
    for (int i = 20000101; i <= 29991231; i++)
    {
        int y = i / 10000;
        int m = i % 10000 / 100;
        int d = i % 100;

        if (m == 0 || m > 12 || d == 0 || d > 31)
            continue;
        if (is_runyear(y))
        {
            if (d <= yr[m])
                jc(i);
        }
        else
        {
            if (d <= nr[m])
                jc(i);
        }
    }
    cout << ans;
    return 0;
}

Output

20000107 20000503 20010223 20010313 20031223 
20060107 20070823 20100907 20130223 20190523 
20190823 20300317 20360317 20400307 20400823 
20480107 20600317 20660617 20700103 20700223 
20700307 20700523 20721013 20910103 20930113 
21000313 21000907 21050503 21320107 21330313 
21360223 21870223 21890107 21990523 23000617 
23010313 23100313 23970313 24021013 24050503 
24090907 24270223 24350503 24501223 24900307 
26001013 26070313 26150503 26190313 26931013 
27020113 27080107 29331013

53

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linengcs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值