输入
1021202
2022310
8102134
1030912
3110203
4021205
#
没有结果输出,统计二三位是02正常应该是3
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
char code[8];
struct node *next;
};
int main(void){
struct node *head,*p;
int count=0,size=sizeof(struct node);
char str[8];
head=NULL;
gets(str);
while(strcmp(str,"#")!=0){
p=(struct node *)malloc (size);
strcpy(p->code,str);
head=p->next;
head=p;
gets(str);
}
for(p=head;p->next!=NULL;p=p->next)
if(p->code[1]=='0'&&p->code[2]=='2')
count++;
printf("%d\n",count);
return 0;
}
head=p->next;
head=p;
这样写肯定是不行啊。head赋值两遍算啥啊。应该是p->next = head; head = p;
修改如下,供参考:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
char code[8];
struct node *next;
};
int main(void)
{
struct node *head,*p;
int count=0,size=sizeof(struct node);
char str[8];
head=NULL;
gets(str);
while(strcmp(str,"#")!=0){
p=(struct node *)malloc (size);
strcpy(p->code,str);
p->next=head; //head=p->next;
head=p;
gets(str);
}
for(p=head;p!=NULL;p=p->next)//for(p=head;p->next!=NULL;p=p->next)
if(p->code[1]=='0'&&p->code[2]=='2')
count++;
printf("%d\n",count);
return 0;
}