Prime Palidromes_迴文素數_usaco1.5_列舉?

NO IMAGE

DESCRIPTION

The number 151 is a prime palindrome because it is both a prime number and a palindrome (it is the same number when read forward as backward). Write a program that finds all prime palindromes in the range of two supplied numbers a and b (5 <= a < b <= 100,000,000); both a and b are considered to be within the range .

PROGRAM NAME: pprime

INPUT FORMAT

Line 1: Two integers, a and b

OUTPUT FORMAT

The list of palindromic primes in numerical order, one per line.

HINTS (use them carefully!)

HINT1 HINT2

TRANSLATION

找到給粗區間[a..b]中的所有迴文素數

ANALYSIS

突然想起還有usaco這麼個題庫 不要在意之前幹嘛去了

咳咳,列舉a~b區間的數字,分別判斷是不是迴文數和素數

玄學優化:
1. 除5外,以5,0結尾的數字是合數,false
2. 除2外,以偶數結尾的數字是合數,false
3. 除11外,偶數數位的迴文數字是合數,false
4. 最大不可能超過9989899 我也不知道為什麼

CODE

/*
ID:wjp13241
PROG:pprime
LANG:C  
*/
#include <stdio.h>
#include <cmath>
using namespace std;
int prime(int x)
{
for (int i=2;i<=(int)(sqrt(x));i  )
if (!(x%i))
return 0;
return 1;
}
int palindrome(int x)
{
int f[10]={0};
while(x)
{
f[  f[0]]=x%10;
x/=10;
}
int l=0;
while (  l<=f[0]/2)
{
if (f[l]!=f[f[0]-l 1])
return 0;
}
return 1;
}
int main()
{
freopen("pprime.in","r",stdin);
freopen("pprime.out","w",stdout);
int a,b;
scanf("%d%d",&a,&b);
a=a==1?2:a;
b=b>9989899?9989899:b;
for (int i=a;i<=b;i  )
{
if (!palindrome(i))
continue;
if (!prime(i))
continue;
printf("%d\n",i);
}
fclose(stdin);
fclose(stdout);
return 0;
}