我的思路就是讀入字串,然后一個字符一個字符的看,建立鏈表,不是新字母就讓對應節點計數器加一,是新字母就新建節點。最后再找到節點中的計數器最大值和最小值,然后判斷差是不是質數。現在的問題是運行不了,求大神指教,代碼如下:
#include<stdio.h>
#include<string.h>
#include<malloc.h>
struct cell{
int x;
char y;
struct cell* next;
};
int zhishu(int n){
int i;
if(n==0||n==1)
return 0;
else{
if(n==2)
return 1;
if(n==3)
return 1;
for(i=2;i<=n-1;i++){
if(n%i==0)
return 0;
}
return 1;
}
}
struct cell* build(char a[],int n){
int i,j,k,l,flag=0;
struct cell *head,*p,*tmp,*h;
head=p=tmp=NULL;
p=(struct cell*)malloc(sizeof(struct cell));
p->next=NULL;
p->y=a[0];
p->x=1;
head=p;
tmp=p;
for(i=1;i<=n-1;i++){
for(h=head;h->next!=NULL;h=h->next){
if(h->y==a[i]){
h->x=h->x+1;
flag=1;
break;
}
}
if(flag==0){
p=(struct cell*)malloc(sizeof(struct cell));
p->next=NULL;
p->x=1;
p->y=a[i];
tmp->next=p;
tmp=p;
}
}
return head;
}
int maxing(struct cell *head){
int flag,i;
struct cell *p;
flag=0;
while(flag==0){
flag==1;
for(p=head;p->next!=NULL;p=p->next){
if((p->x)<(p->next->x)){
flag=0;
i=p->x;
p->x=p->next->x;
p->next->x=i;
}
}
}
return head->x;
}
int mining(struct cell *head){
int flag,i;
struct cell *p;
flag=0;
while(flag==0){
flag==1;
for(p=head;p->next!=NULL;p=p->next){
if(p->x>p->next->x){
flag=0;
i=p->x;
p->x=p->next->x;
p->next->x=i;
}
}
}
return head->x;
}
void release(struct cell *head){
if(head==NULL)
return;
else{
release(head->next);
free(head);
}
}
int main(){
char a[101];
struct cell *head;
int max,min,i,j,k;
scanf("%s",a);
head=build(a,strlen(a));
max=maxing(head);
min=mining(head);
i=max-min;
if(zhishu(i)==0)
printf("No Answer\n0");
else{
printf("Lucky Word\n");
printf("%d",i);
}
release(head);
}
uj5u.com熱心網友回復:
//flag==1;
flag=1;
兩處這樣的賦值
樓主的代碼沒看到創建鏈表,就直接使用鏈表了,肯定是會有問題的~
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/250886.html
標籤:新手樂園
下一篇:請教,變數名儲存在哪里?
