C语言求某个范围内逆序前后都是素数
设计一个程序,输入一个范围,输出范围内本身是素数 反序后还是素数的所有素数。大佬们,这个该怎么弄呀,为什么我这样子弄不行呀。
#include<stdio.h>
int isprime(unsigned begin,unsigned end);
unsigned revers(unsigned begin,unsigned end);
int main() {
int a = 0, b = 0;
printf("请输入一个范围:");
scanf_s("%d%d", &a, &b);
isprime(a, b);
return 0;
}
int isprime(unsigned begin,unsigned end) {
int revers(unsigned begin, unsigned end);
int i = 0;
for (begin; begin <= end; begin++) {
for (i = 2; i < begin; i++) {
if (begin % i == 0) {
return 0;
}
}
}
revers(begin, end);
}
unsigned revers(unsigned begin,unsigned end) {
int t = 0, num = 0,i=0,start=begin,x=1;
while (start > 0) {
t = start % 10;
start = start / 10;
num = num * 10 + t;
}
for (i = 2; i < num; i++) {
if (num % i == 0) {
x = 0;
break;
}
}
if (x == 1){
printf("%d ",begin);
return begin;
}
return 0;
} 本帖最后由 jackz007 于 2022-11-27 16:23 编辑
#include <stdio.h>
int main(void)
{
int a , b , c , i , j , k , n ;
scanf("%d%d" , & a , & b) ;
for(i = a ; i <= b ; i ++) {
for(j = 2 ; j <= i / j ; j ++) if(! (i % j)) break ;
if(j > i / j) {
for(n = i , k = 0 ; n ; n /= 10) k = k * 10 + n % 10 ;
for(c = 2 ; c <= k / c ; c ++) if(! (k % c)) break ;
if(c > k / c && i < k) printf("%d %d\n" , i , k) ;
}
}
}
编译、运行实况:
D:\\C>g++ -o x x.c
D:\\C>x
2 200
13 31
17 71
37 73
79 97
107 701
113 311
149 941
157 751
167 761
179 971
199 991
D:\\C>
页:
[1]