German Collegiate Programming Contest 2015: K. Upside down primes

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33193309/article/details/81054985

Last night, I must have dropped my alarm clock. When the alarm went off in the morning, it showed 51:8051:80instead of 08:1508:15. This made me realize that if you rotate a seven segment display like it is used in digital clocks by 180180 degrees, some numbers still are numbers after turning them upside down.

My favourite numbers are primes, of course. Your job is to check whether a number is a prime and still a prime when turned upside down.

Input Format

One line with the integer NN in question (1 \le N \le 10^{16})(1N1016)NN will not have leading zeros.

Output Format

Print one line of output containing "yes" if the number is a prime and still a prime if turned upside down, "no"otherwise.

样例输入1

151

样例输出1

yes

样例输入2

23

样例输出2

no

样例输入3

18115211

样例输出3

no

注意刚开始也要判断这个数是不是素数

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<queue> 
#include<stdlib.h>
#include<cmath>
using namespace std;  
typedef long long ll;
using namespace std;  
/*ll Quick_Mod(ll a, ll b, ll mod)
{    
    ll res = 1,term = a % mod;    
    while(b)    
    {    
        if(b & 1) res = (res * term) % mod;    
        term = (term * term) % mod;    
        b >>= 1;    
    }    
    return res;    
}    
bool Is_Prime(ll n)    
{    
   int i;    
   srand(time(0));  
   for(i = 0;i < 30;i++)   
       if(Quick_Mod(1+rand()%(n-1) ,n - 1,n) != 1)    
         break;    
   if(i == 30) return true;    
   return false;    
}*/
bool isPrime(long long num) {
    if (num == 2 || num == 3) {
        return true;
    }

    //如果不在6的倍数附近,肯定不是素数
    if (num % 6 != 1 && num % 6 != 5) {
        return false;
    }
    //对6倍数附近的数进行判断
    int fz=sqrt(num)+1;
    for (int i = 5; i <=fz ; i += 6) {
        if (num % i == 0 || num % (i + 2) == 0) {
            return false;
        }
    }
    return true;
}
int main(){
	char s[20];
	scanf("%s",s);
	long long sum=0;
	int len=strlen(s);
	long long sum1=0;
	for(int i=0;i<len;i++){
		sum1=sum1*10+s[i]-'0';
	}
	if(!isPrime(sum1)){
		printf("no\n");
		return 0;
	}
	for(int i=len-1;i>=0;i--){
		if(s[i]=='0'||s[i]=='2'||s[i]=='5'||s[i]=='8'||s[i]=='1')
			sum=sum*10+ s[i]-'0';
		else if(s[i]=='6') sum=sum*10+9;
		else if(s[i]=='9') sum=sum*10+6;
		else{
			printf("no\n");
			return 0;
		}
	} 
	//cout<<sum<<endl;
	if(sum==1){
		printf("no\n");
		return 0;
	}
	if(isPrime(sum)){
		printf("yes\n");
	}
	else printf("no\n");
	return 0;
}



欢迎大家加入 早起学习群,一起学习一起进步!(群号:642179511)

猜你喜欢

转载自blog.csdn.net/qq_33193309/article/details/81054985