求助大神啊c语言两数之间的回文质数,我写的代码哪里有问题呢大神啊...
发布网友
发布时间:2024-10-12 07:33
我来回答
共2个回答
热心网友
时间:5分钟前
用筛选法快速计算素数(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) { /* delete node */
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;
}
热心网友
时间:6分钟前
这个函数有点问题我改了。
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;
}