题解 | #素数回文#
素数回文
https://www.nowcoder.com/practice/d638855898fb4d22bc0ae9314fed956f
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<math.h> int is_single(long long n) { int i = 0; int j = 0; for (i = 2; i < n; i++) { if (n % i == 0) { return 0; } } //只由1-9组成,并且每个数只出现一次,如13,23,1289。 int arr[9] = { 0 }; int count = 0; int m = n; i = 0; while (m) { arr[i] = m % 10; m /= 10; i++; count++; } for (i = 0; i < count; i++) { for (j = 0; j < i; j++) { if (arr[i] == arr[j]) { return 0; } } } int crease = 0; for (i = 0; i < count - 1; i++) { //位数从高到低为递减或递增,如2459,87631。 //计算 if (arr[i] < arr[i + 1]) { crease++; } else if (arr[i] > arr[i + 1]) { crease++; } } if (crease == 0 || crease == count - 1) { return 1; } else { return 0; } } long long check1(long long n) { if (is_single(n)) { //开始回文 int num = 0; long long sum1 = n; while (sum1) { sum1 /= 10; num++; } long long sum2 = n; for (; num > 1; num--) { sum2 = (sum2 * 10 + n / 10 % 10); n /= 10; } return sum2; } else { return 0; } } int check2(long long n) //定义一个函数用来判断素数 { long long a; int b = 0; if (n % 2 == 0) { return 0; } long double c = sqrt(n); for (a = 3; a < c; a += 2) { if ((n % a) == 0) { b++; break; } } if (b == 0) { return 1; } else { return 0; } } int main() { long long n = 0; scanf("%lld", &n); n = check1(n); if (n == 0) { printf("搞错啦!搞错啦!!!\n"); return 1; } n = check2(n); if (n) { printf("prime\n"); } else { printf("noprime\n"); } return 0; }