#include<stdio.h>
int isprime(int a)
{
int i,m;
for(i=2,m=0;i<a;i++)
{
if(a%i!=0)
continue;
m++;
}
if(m==0)
{
return 1;}
}
int ishuiwen(int b,int c[7])
{
int j,m1,j1;
int z=1;
for(j=0,m1=0;j<7;j++)
{
c[j]=b%10;
b/=10;
m1++;
}
for(j=0;j<m1-1;j++)
{
if(c[j]!=c[m1-1-j])
{
z=0;break;}
else
continue;
}
return z;
}
int main()
{
int n1,n2,n3,t,k,c[7];
scanf("%d %d",&n1,&n2);
if(n1>n2)
{
t=n2;
n2=n1;
n1=t;
}
for(n3=n1;n3<=n2;n3++)
{
if(isprime(n3)&&ishuiwen(n3,c[7]))
{
printf("%d\n",n3);
}
}
return 0;
}
求助大神啊c语言两数之间的回文质数,我写的代码哪里有问题呢大神啊?
答案:3 悬赏:10
解决时间 2021-03-05 20:47
- 提问者网友:樱花树下最美的约定
- 2021-03-05 16:41
最佳答案
- 二级知识专家网友:嗷呜我不好爱
- 2021-03-05 17:48
用筛选法快速计算素数(100000数量级实测400ms左右)然后打印:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
struct node_s;
typedef struct node_s {
int data;
struct node_s *next;
} node_t;
inline int isbalance(int data)
{
static int array[20];
int head, tail, cnt=0;
while(data)
{
array[cnt++] = data%10;
data /= 10;
}
for (head=0,tail=(cnt-1);tail > head;head++,tail--)
{
if (array[head] != array[tail]){
return 0;
}
}
return 1;
}
void print_list(node_t *head, int start)
{
node_t *ptr = head->next;
int count = 0, total = 0;;
while(ptr) {
if (ptr->data >= start) {
total ++;
if(isbalance(ptr->data)){
count ++;
printf("%d,", ptr->data);
}
}
ptr=ptr->next;
}
printf("\ncount/total: %d/%d\n", count, total);
}
void process_list(node_t *head)
{
node_t *ptr = head->next, *ptmp, *prev;
while(ptr) {
ptmp = ptr->next;
prev = ptr;
while(ptmp) {
if (ptmp->data%ptr->data == 0) {
prev->next = ptmp->next;
} else {
prev = ptmp;
}
ptmp = prev->next;
}
ptr = ptr->next;
}
}
int main()
{
int i,end,start;
long tstart, tend;
node_t *pbuf, *head;
printf("input start and end:\n");
scanf("%d %d", &start, &end);
tstart = GetTickCount();
pbuf = (node_t *)malloc(sizeof(node_t)*(end+1));
head = &pbuf[1];
for (i=2;i<=end;i++) {
pbuf[i-1].next = &pbuf[i];
pbuf[i].data = i;
pbuf[i].next = NULL;
}
process_list(head);
tend = GetTickCount();
printf("process complete, time use: %ld msec\n",
tend - tstart);
print_list(head, start);
system("pause");
free(pbuf);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
struct node_s;
typedef struct node_s {
int data;
struct node_s *next;
} node_t;
inline int isbalance(int data)
{
static int array[20];
int head, tail, cnt=0;
while(data)
{
array[cnt++] = data%10;
data /= 10;
}
for (head=0,tail=(cnt-1);tail > head;head++,tail--)
{
if (array[head] != array[tail]){
return 0;
}
}
return 1;
}
void print_list(node_t *head, int start)
{
node_t *ptr = head->next;
int count = 0, total = 0;;
while(ptr) {
if (ptr->data >= start) {
total ++;
if(isbalance(ptr->data)){
count ++;
printf("%d,", ptr->data);
}
}
ptr=ptr->next;
}
printf("\ncount/total: %d/%d\n", count, total);
}
void process_list(node_t *head)
{
node_t *ptr = head->next, *ptmp, *prev;
while(ptr) {
ptmp = ptr->next;
prev = ptr;
while(ptmp) {
if (ptmp->data%ptr->data == 0) {
prev->next = ptmp->next;
} else {
prev = ptmp;
}
ptmp = prev->next;
}
ptr = ptr->next;
}
}
int main()
{
int i,end,start;
long tstart, tend;
node_t *pbuf, *head;
printf("input start and end:\n");
scanf("%d %d", &start, &end);
tstart = GetTickCount();
pbuf = (node_t *)malloc(sizeof(node_t)*(end+1));
head = &pbuf[1];
for (i=2;i<=end;i++) {
pbuf[i-1].next = &pbuf[i];
pbuf[i].data = i;
pbuf[i].next = NULL;
}
process_list(head);
tend = GetTickCount();
printf("process complete, time use: %ld msec\n",
tend - tstart);
print_list(head, start);
system("pause");
free(pbuf);
return 0;
}
全部回答
- 1楼网友:绝望伪装
- 2021-03-05 19:32
这个函数有点问题我改了。
int ishuiwen(int b,int c[7])
{
int j,m1;
int z=1;
for(j=0,m1=0;j<7;j++)
{
c[j]=b%10;
b/=10;
m1++;
if(b == 0)
break;
}
for(j=0;j<m1 / 2;j++)
{
if(c[j]!=c[m1-1-j])
{
z=0;break;}
else
continue;
}
return z;
}
- 2楼网友:蜜罐小熊
- 2021-03-05 18:09
这个函数有点问题我改了。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯