当前位置: 代码迷 >> C语言 >> Prime Palindromes有错
  详细解决方案

Prime Palindromes有错

热度:408   发布时间:2007-12-17 21:09:51.0
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  

----------------解决方案--------------------------------------------------------
你做USACO啊?已经做到1.5了?
----------------解决方案--------------------------------------------------------
嗯,看了一下,有两个主要错误如下:

#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;
}
----------------解决方案--------------------------------------------------------
哦 原来是这样的哦 谢谢哈
程序代码:
#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]]
----------------解决方案--------------------------------------------------------
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
----------------解决方案--------------------------------------------------------