超级素数
100分写法——对5个点
#include<iostream>
using namespace std;
int cnt, n;
int a[] = {0, 2, 3, 5, 7}, b[] = {0, 1, 3, 7, 9};
bool prime(int n)
{
if(n < 2) return false;
for(int i = 2; i <= n / i; i++)
if(n % i == 0)
return false;
return true;
}
void search(int s)
{
if(s > n) return;
if(prime(s))
cnt++;
else
return;
for(int i = 1; i <= 4; i++)
search(s * 10 + b[i]);
}
int main()
{
cin >> n;
for(int i = 1; i <= 4; i++)
search(a[i]);
cout << cnt << "\n";
return 0;
}
80分普通写法——对4个点
#include<iostream>
using namespace std;
int n, cnt;
bool prime(int n)
{
if(n < 2) return false;
for(int i = 2; i <= n / i; i++)
if(n % i == 0)
return false;
return true;
}
bool check(int x)
{
while(x)
{
if(!prime(x)) return false;
x /= 10;
}
return true;
}
int main()
{
cin >> n;
for(int i = 1; i <= n; i++)
{
if(check(i))
cnt++;
}
cout << cnt;
return 0;
}