topic
Description
Input an integer n (2<=n<=10000), it is required to output all prime numbers from 1 to this integer (excluding 1 and this integer) with one digit 1, if not, output -1.
Input
There are multiple sets of data entered.
One line per group, enter n.
Output
Output all prime numbers from 1 to this integer (not including 1 and this integer) with one digit 1 (the prime numbers are separated by spaces, and there is no space after the last prime number). If not, output -1.
Ideas
Since n<=10000, you can consider typing the prime number table before outputting.
Note: If there is no prime number (that is, n<=11), output -1
Code
#include <iostream>
#include <stdio.h>
#include <vector>
#include <math.h>
using namespace std;
vector<int> primes;
bool isPrime(int n){
int sqr=sqrt(1.0*n);
for(int i=2;i<=sqr;i++)
if(n%i==0)
return false;
return true;
}
void FindPrime(){
for(int i=11;i<10000;i+=10)//注意只需要个位为1的素数
if(isPrime(i))
primes.push_back(i);
}
int main(){
int n;
FindPrime();
while(scanf("%d",&n)!=EOF){
if(n<=11){
printf("%d\n",-1);
continue;
}
for(int i=0;primes[i]<n&&i<=305;i++){
if(primes[i+1]>=n||i==305)//注意最后一个素数后面没有空格
printf("%d",primes[i]);
else
printf("%d ",primes[i]);
}
printf("\n");
}
return 0;
}