| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 932 人关注过本帖, 1 人收藏
标题:Prime Palindromes有错
只看楼主 加入收藏
hago
Rank: 1
等 级:新手上路
帖 子:52
专家分:0
注 册:2007-11-3
收藏(1)
 问题点数:0 回复次数:4 
Prime Palindromes有错
程序代码:
#include<stdio.h>
#include<math.h>
int isprime(long x)    /*判断是否为素数*/
{
    long i,j;
    
    i=(int)sqrt(x)+1;
    for(j=0;j<=i;j++)
        if(x%j==0){
            return 0;
            break;
        }
    if(j>i)
        return 1;
}
int ispalindromes(long x)    /*判断是否为回文数*/
{
    int i;
    long num=0,j=x;
    
    while(j){
        i=j%10;
        j=j/10;
        num=num*10+i;
    }
    if(j==num) return 1;
    if(j!=num) return 0;
}
int main()
{
    long bot,top,i;
    
    scanf("%ld %ld",&bot,&top);
    for(i=bot;i<=top;i++){
        while(isprime(i))
            while(ispalindromes(i))
                printf("%ld\n",i);
    }
    return 0;
}
Prime Palindromes程序 不知错在哪 望哪位高手指教下...
搜索更多相关主题的帖子: Prime Palindromes 
2007-12-17 21:09
leeco
Rank: 4
等 级:贵宾
威 望:10
帖 子:1029
专家分:177
注 册:2007-5-10
收藏
得分:0 
你做USACO啊?已经做到1.5了?
2007-12-17 22:09
blueboy82006
Rank: 5Rank: 5
来 自:幻想世界
等 级:贵宾
威 望:16
帖 子:1227
专家分:57
注 册:2007-7-23
收藏
得分:0 
嗯,看了一下,有两个主要错误如下:

#include<stdio.h>
#include<math.h>
int isprime(long x)    /*判断是否为素数*/
{
    long i,j;
   
    i=sqrt(x);
    for(j=2;j<=i;j++)  ////j从2开始,你怎么能从0开始呢?很严重的错误!
        if(x%j==0){
            return 0;
            break;
        }
    if(j>i)
        return 1;
}
int ispalindromes(long x)    /*判断是否为回文数*/
{
    int i;
    long num=0,j=x,a=x;
   
    while(j){
        i=j%10;
        j=j/10;
        num=num*10+i;
    }
    if(a==num) return 1; /////你写的是j==num我想问一下,现在的j还是那个j=x吗???
    else return 0;
}
int main()
{
    long bot,top,i;
   
    scanf("%ld,%ld",&bot,&top);
    for(i=bot;i<=top;i++){
        if(isprime(i) && ispalindromes(i))
             printf("%ld\n",i);
    }
    return 0;
}

2007-12-17 22:49
hago
Rank: 1
等 级:新手上路
帖 子:52
专家分:0
注 册:2007-11-3
收藏
得分:0 
哦 原来是这样的哦 谢谢哈
程序代码:
#include<stdio.h>
#include<math.h>
int isprime(long int x)    /*判断是否为素数*/
{
    long int j;
    
    for(j=2;j<=(int)sqrt(x);j++)
        if(x%j==0)
            return 0;
        return 1;
}
int ispalindromes(long int x)    /*判断是否为回文数*/
{
    long int num=0,y=x;
    
    for(;y;y/=10)
        num=num*10+y%10;
    return (x==num);
}
int main(void)
{
    long bot,top,i,j;
    double D=pow(10,8);
    
    scanf("%ld %ld",&bot,&top);
    for(i=bot;i<=top;i++){
        if(i<2*D||(i>3*D&&i<4*D)||(i>7*D&&i<=8*D)||i>=9*D)
        {
            if(isprime(i))
                if(ispalindromes(i))
                    printf("%ld\n",i);
        }
    }
    return 0;
}
程序对了 但是运行时间长了  通不过啊
 谁有好的算法???

[[italic] 本帖最后由 hago 于 2007-12-17 23:57 编辑 [/italic]]
2007-12-17 23:56
hago
Rank: 1
等 级:新手上路
帖 子:52
专家分:0
注 册:2007-11-3
收藏
得分:0 
Prime Palindromes

--------------------------------------------------------------------------------

Time limit: 15sec. Submitted: 3927
Memory limit: 32M Accepted: 577

Source : USACO Gateway

--------------------------------------------------------------------------------

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 <= 1000,000,000); both a and b are considered to be within the range .

Input
Line 1: Two integers, a and b

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

Sample Input
5 500

Sample Output
5
7
11
101
131
151
181
191
313
353
373
383
2007-12-18 01:07
快速回复:Prime Palindromes有错
数据加载中...
 
   



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

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