标题:[求助]这是一个关于回数的程序,是我们的作业 ,可是却做不出来
取消只看楼主
雾雨非流云
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2007-11-3
 问题点数:0 回复次数:2 
[求助]这是一个关于回数的程序,是我们的作业 ,可是却做不出来

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 3 and 1,000,000,000.

Output
The list of palindromic primes in numerical order, one per line. Then print the run time (seconds) at the last line.


Sample Output

3
5
7
11
101
131
151
181
191
313
353
373
383
...
...
30 seconds The final score


The final score Score = Style(10) + Correct(50) + Speed(T)
where T = 70 - RunTime(seconds)

搜索更多相关主题的帖子: 作业 prime number The 
2007-11-07 14:09
雾雨非流云
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2007-11-3
得分:0 

谢谢大家
我已经编出来了
只运行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;

}


2007-11-09 10:52
雾雨非流云
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2007-11-3
得分:0 

这次作业花了我一周的时间和汗水啊
太不容易了
我决定好好奖励一下自己
呵呵

2007-11-09 11:29



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-184219-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024756 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved