USACO Section 1.5 Prime Palindromes

来源:互联网 发布:维护数据库培训班 编辑:程序博客网 时间:2024/05/19 07:10

题目描述

151号是一个主要的回文,因为它是素数和回文(当向前读取时,它是相同的数字)。 编写一个程序,找到两个提供的数字a和b(5 <= a <b <= 100,000,000)范围内的所有主回文; a和b都被认为在该范围内。

程序名称:pprime
输入格式

行1:两个整数,a和b

输入 (file pprime.in)

5 500

输出格式

数字序列中的回文素数列表,每行一个

输出(file pprime.out)

5711101131151181191313353373383

提示(仔细使用它们)!

提示1 提示2

解题代码

/*ID: 15189822PROG: pprimeLANG: C++*/#include<iostream>#include<cmath>#include<fstream>using namespace std;ifstream fin("pprime.in");ofstream fout("pprime.out");const int N = 1000;long x[N+1];int cnt=0;long a,b;bool is_prime(long n){   if (n<a||n>b){       return false;   }   if (n==0||n==1) return false;   if (n==2||n==3) return true;   long i;   for (i=2;i<=sqrt(n);i++){      if (n%i==0) return false;   }   return true;}void hui_8(){     int x1,x2,x3,x4;    for (x1=1;x1<=9;x1+=2){        for (x2=0;x2<=9;x2++){            for (x3=0;x3<=9;x3++){                for (x4=0;x4<=9;x4++){                    long n=(x1*1000+x2*100+x3*10+x4)*10000+x4*1000+x3*100+x2*10+x1;                    if (is_prime(n)) fout<<n<<endl;                }            }        }    }}void hui_7(){    int x1,x2,x3,x4;    for (x1=1;x1<=9;x1+=2){        for (x2=0;x2<=9;x2++){            for (x3=0;x3<=9;x3++){                for (x4=0;x4<=9;x4++){                    long n=(x1*1000+x2*100+x3*10+x4)*1000+x3*100+x2*10+x1;                    if (is_prime(n)) fout<<n<<endl;                }            }        }    }}void hui_6(){    int x1,x2,x3;    for (x1=1;x1<=9;x1+=2){        for (x2=0;x2<=9;x2++){            for (x3=0;x3<=9;x3++){                long n=(x1*100+x2*10+x3)*1000+x3*100+x2*10+x1;                if (is_prime(n)) fout<<n<<endl;            }        }    }}void hui_5(){    int x1,x2,x3;     for (x1=1;x1<=9;x1+=2){        for (x2=0;x2<=9;x2++){            for (x3=0;x3<=9;x3++){                long n=(x1*100+x2*10+x3)*100+x2*10+x1;                if (is_prime(n)) fout<<n<<endl;            }        }    }}void hui_4(){    int x1,x2;    for (x1=1;x1<=9;x1+=2){        for (x2=0;x2<=9;x2++){            long n=(x1*10+x2)*100+x2*10+x1;            if (is_prime(n)) fout<<n<<endl;        }    }}void hui_3(){    int x1,x2;    for (x1=1;x1<=9;x1+=2){        for (x2=0;x2<=9;x2++){            long n=(x1*10+x2)*10+x1;            if (is_prime(n)) fout<<n<<endl;        }    }}void hui_2(){    int x1;    for (x1=1;x1<=9;x1+=2){        long n=x1*10+x1;        if (is_prime(n)) fout<<n<<endl;    }}void hui_1(){    for (int i=2;i<=9;i++){        if (is_prime(i)) fout<<i<<endl;    } }int main(){    cnt=0;    fin>>a>>b;    hui_1();    hui_2();     hui_3();     hui_4();     hui_5();     hui_6();    hui_7();     hui_8();    return 0;}


原创粉丝点击