您的位置:首页 > 其它

[USACO1.5]回文质数 Prime Palindromes

2017-06-15 21:02 561 查看
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

SAMPLE INPUT (file pprime.in)

5 500

OUTPUT FORMAT

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

SAMPLE OUTPUT (file pprime.out)

5
7
11
101
131
151
181
191
313
353
373
383

HINTS (use them carefully!)

       HINT 1  
       HINT 2   

Prime Palindromes 回文质数


目录

 [隐藏
1 描述
2 格式
3 SAMPLE
INPUT
4 SAMPLE
OUTPUT
5 提示
HINTS(小心使用 use them carefully!)


[编辑]描述

因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数。

写一个程序来找出范围[a,b](5 <= a < b <= 100,000,000)( 一亿)间的所有回文质数;


[编辑]格式

PROGRAM NAME: pprime

INPUT FORMAT:

(file pprime.in)

第 1 行: 二个整数 a 和 b .

OUTPUT FORMAT:

(file pprime.out)

输出一个回文质数的列表,一行一个。


[编辑]SAMPLE
INPUT

5 500


[编辑]SAMPLE
OUTPUT

5
7
11
101
131
151
181
191
313
353
373
383


[编辑]提示
HINTS(小心使用 use them carefully!)

Hint 1: Generate the palindromes and see if they are prime. 提示 1: 找出所有的回文数再判断它们是不是质数(素数).

Hint 2: Generate palindromes by combining digits properly. You might need more than one of the loops like below. 提示 2: 要产生正确的回文数,你可能需要几个像下面这样的循环。

产生长度为5的回文数: 
C++:


for (d1 = 1; d1 <= 9; d1+=2) {	// 只有奇数才会是素数
for (d2 = 0; d2 <= 9; d2++) {
for (d3 = 0; d3 <= 9; d3++) {
palindrome = 10000*d1 + 1000*d2 +100*d3 + 10*d2 + d1;//(处理回文数...)
}
}
}


【题解】

刚刚写的代码

/*
ID:luojiny1
LANG:C++
TASK:pprime
*/
#include<cstdio>
bool ok(int x)
{
for(int i=2;i*i<=x;i++)
if(x%i==0)return false;
return true;
}
int main()
{
int a,b;
freopen("pprime.in","r",stdin);
freopen("pprime.out","w",stdout);
scanf("%d%d",&a,&b);
if(a<=2&&b>=2)printf("2\n");
if(a<=3&&b>=3)printf("3\n");
if(a<=5&&b>=5)printf("5\n");
if(a<=7&&b>=7)printf("7\n");
if(a<=11&&b>=11)printf("11\n");
for(int i=1;i<=9;i+=2)
for(int j=0;j<=9;j++){//3位
int s=i*101+j*10;if(ok(s)&&s>=a&&s<=b)printf("%d\n",s);
}
for(int i=1;i<=9;i+=2)
for(int j=0;j<=9;j++)
for(int k=0;k<=9;k++){//5位
int s=i*10001+j*1010+k*100;if(ok(s)&&s>=a&&s<=b)printf("%d\n",s);
}
for(int i=1;i<=9;i+=2)
for(int j=0;j<=9;j++)
for(int k=0;k<=9;k++)
for(int l=0;l<=9;l++){//7位
int s=i*1000001+j*100010+10100*k+l*1000;if(ok(s)&&s>=a&&s<=b)printf("%d\n",s);
}
for(int i=1;i<=9;i+=2)
for(int j=0;j<=9;j++)
for(int k=0;k<=9;k++)
for(int l=0;l<=9;l++)
for(int m=0;m<=9;m++){//9位
int s=i*100000001+j*10000010+1000100*k+l*101000+m*10000;if(ok(s)&&s>=a&&s<=b)printf("%d\n",s);
}
return 0;
}

long long ago....
#include<cstdio>

int a;
bool pd(int num)
{
for(int i=2;i*i<=num;i++)
if(num%i==0)return false;
return true;
}
int main()
{
int t=0,tt=-1,x,y;
scanf("%d%d",&x,&y);
for(int i=0;i<=11;i++)
if(pd(i)&&i>=x&&i<=y)
printf("%d\n",i);
else if(a>y)return 0;
for(int i1=0;i1<=9;i1++)
for(int i2=0;i2<=9;i2++)
for(int i3=0;i3<=9;i3++)
for(int i4=0;i4<=9;i4++)
for(int i5=0;i5<=9;i5++)
{
if(!i1&&!i2&&!i3)a=i4*101+i5*10;
else if(!i1&&!i2)a=i3*10001+i4*1010+i5*100;
else if(!i1)a=i2*1000001+i3*100010+i4*10100+i5*1000;
else a=i1*100000001+i2*10000010+i3*1000100+i4*101000+10000;
if(pd(a)&&a>=x&&a<=y)
printf("%d\n",a);
else if(a>y)return 0;
}
return 0;
}

说多了都是泪,当年的思路很清晰。。现在堕落了&_& 刷...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: