不好意思,看错了....
----------------解决方案--------------------------------------------------------
对应30楼用DEV C++4.9.9.2编译的可执行文件
----------------解决方案--------------------------------------------------------
30楼的程序已经很不错了,LZ交作业拿个100分以上是没问题的,我的vc也就跑了10秒
----------------解决方案--------------------------------------------------------
谁的机器能在30秒内算完?????
我肯定是完成不了
----------------解决方案--------------------------------------------------------
30楼的程序
我跑3秒
[此贴子已经被作者于2007-11-8 21:29:32编辑过]
----------------解决方案--------------------------------------------------------
可以证明: 除了11,不存在偶数位的素数回文数
so,qq95620412的代码还可以优化,另外,素数判断的代码还可以进一步优化.
----------------解决方案--------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int sushu(long x)//判断数是否是素数,若不为素数输出0,否则输出不为0的数
{
long a=1;
int c=1;
double k;k=sqrt(x);
while(c&&a<k+1)
{
a++;c=x%a;
}
return c;
}
int nixushu(long x)//求一个数的逆序数
{
int a;
long b=0;
while(x)
{
a=x%10;x=x/10;b=b*10+a;
}
return b;
}
int main()
{
long a;
double D=pow(10,8);
for(a=3;a<10*D;a+=2)
{
if(a<2*D||(a>3*D&&a<4*D)||(a>7*D&&a<=8*D)||a>=9*D)//数a开头为24568的不计入
{
if(nixushu(a)==a)
{
if(sushu(a))
{
printf("%ld\n",a);
}
}
}
}
printf("------END----________--------");
return 0;
}
----------------解决方案--------------------------------------------------------
我的代码:
/**
@author Eastsun
*/
#include<stdio.h>
#include<time.h>
#define MAX_NUMBER 1000000000
#define MAX_SQL 31622
#define MAX_LENGTH 9
long prime[MAX_SQL];
int primeCount =0;
void listAll();
int main(){
time_t start,end;
start =clock();
listAll();
end = clock();
printf(\"%ld seconds\n\",(end -start)/CLOCKS_PER_SEC);
}
int isPrime(long n){
long number;
int flags,index;
if(primeCount ==0){
for(number =2;number<= MAX_SQL;number ++){
flags =1;
for(index =0;index< primeCount;index ++)
if(number %prime[index]==0){
flags =0;
break;
}
if(flags) prime[primeCount++] =number;
}
}
flags =1;
for(index =0;index<primeCount;index ++){
number =prime[index];
if(number*number>n) break;
if(n%number==0){
flags =0;
break;
}
}
return flags;
}
int inverse(int n){
int tmp =0;
while(n){
tmp =tmp *10 +n%10;
n /=10;
}
return tmp;
}
void listAll(){
int length,max,min,index,inv,half,mid;
long number;
for(number =3;number <=11;number ++)
if(isPrime(number)) printf(\"%ld\n\",number);
for(int length =3;length<=MAX_LENGTH;length +=2){
max =1;
for(index =length/2;index>0;index --) max *= 10;
min =max/10;
for(half =min;half<max;half++){
inv =inverse(half);
if(inv&1==0) continue;
for(mid =0;mid< 10;mid ++){
number =(half*10 +mid)*max +inv;
if(isPrime(number)) printf(\"%ld\n\",number);
}
}
}
}
----------------解决方案--------------------------------------------------------
谢谢大家
我已经编出来了
只运行3秒
大家看一下 发表一下意见 呵呵
#include <math.h>
#include <stdio.h>
#include <time.h>
/*打印3 - 1000000000的回文素数*/
/*打印出运行时间*/
/*打印出回文素数的个数*/
int Prime(int x);
int main()
{
int count = 4;
int a, b, c, d, e, f, g, h;
g = time(NULL);
printf("3\n5\n7\n11\n");
for (a = 1; a <= 9; a += 2)/*实现三位数的回文素数*/
{
for (b = 0; b <= 9; b++)
{
f = a * 101 + b * 10;
if (Prime(f))
{
count++;
printf("%d\n", f);
}
}
}
for (a = 1; a <= 9; a += 2)/*实现五位数的回文素数*/
{
for (b = 0; b <= 9; b++)
{
for (c = 0; c <= 9; c++)
{
f = a * 10001 + b * 1010 + c * 100;
if (Prime(f))
{
count++;
printf("%d\n", f);
}
}
}
}
for (a = 1; a <= 9; a += 2)/*实现七位数的回文素数*/
{
for (b = 0; b <= 9; b++)
{
for (c = 0; c <= 9; c++)
{
for (d = 0; d <= 9; d++)
{
f = a * 1000001 + b * 100010 + c * 10100 + d * 1000;
if (Prime(f))
{
count++;
printf("%d\n", f);
}
}
}
}
}
for (a = 1; a <= 9; a += 2)/*实现九位数的回文素数*/
{
for (b = 0; b <= 9; b++)
{
for (c = 0; c <= 9; c++)
{
for (d = 0; d <= 9; d++)
{
for (e = 0; e <= 9; e++)
{
f = a * 100000001 + b * 10000010 + c * 1000100 + d * 101000 +e * 10000;
if (Prime(f))
{
count++;
printf("%d\n", f);
}
}
}
}
}
}
h = time(NULL);
printf("time = %d seconds\n",(h - g));
printf("count = %d\n",count);
return 0;
}
//函数功能:判断素数
//函数参数:整数
//函数返回值: 0 或 1
int Prime(int x)
{
int i;
double k;
k=sqrt(x);
for (i = 3;i <= k; i++)
{
if (x % i==0)
break;
}
if (i > k)
return 1;
else
return 0;
}
----------------解决方案--------------------------------------------------------
这次作业花了我一周的时间和汗水啊
太不容易了
我决定好好奖励一下自己
呵呵
----------------解决方案--------------------------------------------------------