我们将所有日期为质数的天数定义为质数天。 例如: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