中易网

求助大神啊c语言两数之间的回文质数,我写的代码哪里有问题呢大神啊?

答案:3  悬赏:10  
解决时间 2021-03-05 20:47
#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;
}
最佳答案
用筛选法快速计算素数(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;
}
全部回答
这个函数有点问题我改了。 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; }
这个函数有点问题我改了。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
马上装修了 餐厅有点小餐桌得贴墙放 不想贴墙
杜兰特真高啊,我昨天看他比赛,他比凯文勒夫
凤台县朱马店地税局在哪里啊,我有事要去这个
我的手机是中国移动, 24号是我的月结日,为
xml,php,css,js,less这些都有什么意思?
请问各位,当今社会,嫁到农村好还是城里好,
怎么让手机用剑侠情缘网游的表情?微信或者某
鑫诚钣金喷漆地址有知道的么?有点事想过去
想入个xboxone手柄,请问现在pc新老游戏支持
潮白人家南区是燕郊八中招生范围的吗
我08年开过证券户,以后一直没用,也没销户,
良益思货运地址在哪,我要去那里办事
coup de foudre 读音
经常上厕所就是肾不好吗?如果肾不好会有什么
我问一下就是长城哈佛有24小时免费上门维修的
推荐资讯
window 无法创建快捷方式。请检查磁盘是否已
梦见和朋友同搞一女人
刚买回来的蜥蜴不吃食,怎么办?
小学六年级数学基础训练44页答案是什么
C++一个函数一个偶数可以表示为两个质数之和
一个男人对我说想和我有个美好的未来可以吗是
环站南路地址在哪,我要去那里办事
汇景便利店地址有知道的么?有点事想过去
11选5今天的开奖结果谢谢各位了
多客酒店地址有知道的么?有点事想过去
苹果4升级开不了机,去手机店刷机大概要多久
隆盛精品窗帘这个地址在什么地方,我要处理点
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?