| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 398 人关注过本帖
标题:有人给我跑一下这个程序吗?
只看楼主 加入收藏
camelbupt
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2007-9-28
收藏
 问题点数:0 回复次数:6 
有人给我跑一下这个程序吗?
[求助]这是一个关于回数的程序,是我们的作业 ,可是却做不出来

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.

我写了一个,大家帮我run一下.
#include "stdio.h"
int main()
{
int i,n,j,k,count=0,lower,upper,integer,array[10];
for(i=100;i<1000000000;++i)
{
integer=i;
n=(int) integer/2;
for(j=2;j<=n;++j)
if(i%j==0)
break;
if(j>n)
{
k=0;
while((integer%10!=0) ||(integer/10!=0))
{
array[k]=integer%10;
integer=integer/10;
k++;
}
lower=0;
upper=--k;
while(lower<upper)
{
if(array[lower]!=array[upper])
break;
lower++;
upper--;
}
if(lower>=upper)
{
printf("%10d",i);
count++;
}
if(count==8)
{
printf("\n");
count=0;
}
}
}
return 0;
}



2007-11-09 23:30
camelbupt
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2007-9-28
收藏
得分:0 
我跑到98689就跑不动了。
2007-11-09 23:31
camelbupt
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2007-9-28
收藏
得分:0 
怎么没反映吗?
能跑结束ma?
在线等....
2007-11-09 23:37
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 


/* 编写者 qq95620412 */

#include<stdio.h>
#include<math.h>
#include<time.h>

#define MAX 1000000000
#define MIN 3

long int pow10(int i)
//因为DEV C++中的math.h没有 pow10函数,所以现编了一个
//输入:int型整数
//输出:返回10的该整数次幂,返回类型为long int。
{
long int result=1;
while(i>0)
{
result*=10;
i--;
}
return result;
}

long int nexthuiwen(long int currenthuiwen)
//输入:一个回文数
//输出:比当前回文数大的最小回文数
{
long int i=currenthuiwen,m;
int weishu=0; //数字的位数
long int j=0;
// printf("当前回文数是:%ld\n",i);

while(i>0) //求出数值的位数
{
i=i/10;
weishu+=1;
}

if(weishu==1) //求出规整前的新数
{
currenthuiwen+=1;
}
else
{
currenthuiwen+=pow10(weishu/2);
currenthuiwen=currenthuiwen-currenthuiwen%pow10(weishu/2);
}
// printf("新数为:%ld\n",currenthuiwen);

i=currenthuiwen; //求出规整前数值的位数
weishu=0;
while(i>0)
{
i=i/10;
weishu+=1;
}

for(j=1;j<=weishu/2;j++) //规整新数
{
m=currenthuiwen/(long int)pow10(weishu-j)%10*(long int)pow10(j-1);
// printf("%ld\n",m);
currenthuiwen+=m;
}
// printf("新的回文数是: %ld\n",currenthuiwen);
return currenthuiwen;
}


int sushu(long int data)
//输入:任意长整数
//输出:若该整数为素数,则输出1,否则输出0
{
long int end=(long int)sqrt(data);
long int i;
for(i=2;i<=end;i++)
{
if(data%i==0)return 0;
}
return 1;
}


int main(void)
{
long int data=MIN;
clock_t start,end;
start=clock();
while(data<=MAX)
{
if(sushu(data)) printf("%ld\n",data);
data=nexthuiwen(data);
}
end=clock();
printf("%lf\n",(double)((end-start)/CLOCKS_PER_SEC));
return 0;
}

/* 这个程序我只跑3秒,DEV-C++ */


—>〉Sun〈<—
2007-11-09 23:39
camelbupt
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2007-9-28
收藏
得分:0 
hehe,帮我跑一下我写的萨!!!
看看能全部跑下来不?
我机子跑到一半就死机了
2007-11-09 23:40
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
你还是研究 qq95620412 的程序吧。

穷举法不适合这个题目,我也编过一个这样的穷举的。(i 从 3 开始递增)
我自己写的都没有把它跑到结束。(实在太慢了)

还有这个程序因该从 3 开始,而不是从 100 开始

3
5
7
11
101
131
151
......
......
999686999
999727999


有人发过这个题了

http://bbs.bc-cn.net/viewthread.php?tid=184219

这帖子中 38 楼 程序超快

[此贴子已经被作者于2007-11-10 0:17:48编辑过]


—>〉Sun〈<—
2007-11-09 23:50
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
以下是引用camelbupt在2007-11-9 23:31:52的发言:
我跑到98689就跑不动了。

可以跑完的,
只是要等很久,别把时间浪费在等待上面,
看看别人写的程序吧!


—>〉Sun〈<—
2007-11-10 00:09
快速回复:有人给我跑一下这个程序吗?
数据加载中...
 
   



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

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