1015. Reversible Primes (20)

the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1015

this problem is relatively easy.if there must be something which need to be noticed, i think

“1 is not a prime” could be one of them.

#include<stdio.h>



bool isPrime(int n)

{

    if (n == 1)

    {

        return false;

    }

    if (n == 2)

        return true;

    else

    {

        for (int i = 2; i < n; i++)

        {

            if (n % i == 0)

            {

                return false;

            }

        }

        return true;

    }

}



int main()

{

    int n,d;

    while (scanf("%d",&n))

    {

        if (n < 0)

            break;

        scanf("%d",&d);

        int temp = n;

        int count = 0;

        while (n/d != 0)

        {

            count += n%d;

            count *= d;

            n /= d;

        }

        count += n%d;

        if (isPrime(temp)&&isPrime(count))

        {

            printf("Yes\n");

        }

        else

            printf("No\n");

    }

}

你可能感兴趣的:(Prim)