链表(2)

分数线划定:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 typedef struct node
 4 {
 5     int bmh;
 6     int score;
 7     struct node* next;
 8 }Node;
 9 
10 void insert(Node *head,Node *s);
11 void output(Node *head,int m);
12 void destory(Node *head);
13 
14 int main (void)
15 {
16     Node *head,*p,*s,*prep;
17     head = (Node*)malloc(sizeof(Node));
18     head->next = NULL;
19     int n,m;
20     scanf("%d %d",&n,&m);
21     m = m*1.5;
22     for(int i = 1;i <= n;i++)
23     {
24         s = (Node*)malloc(sizeof(Node));
25         scanf("%d %d",&s->bmh,&s->score);
26         insert(head,s);
27     }
28     output(head,m);
29     destory(head);
30     return 0;
31 }
32 void insert(Node *head,Node *s)
33 {
34     Node *p = head->next;
35     Node *prep = head;
36     while(p&&(p->score>s->score||(p->score == s->score&&p->bmh<s->bmh)))
37     {
38         prep = p;
39         p = p->next;
40     }
41     s->next = p;
42     prep->next = s;
43 }
44 
45 void output(Node *head,int m)
46 {
47     int c = 0;//统计进入面试的人数
48     Node *p = head->next;//p指向第一个节点
49     int ss;//进入面试的成绩
50     while(p)
51     {
52         c++;
53         if(c == m)//这时p指向了第m个节点
54         {
55             ss = p->score;//面试成绩 
56             Node *s = p;//为什么要这一步 
57             p = p->next;
58             while(p->score == ss)//把与第m个节点分数相同的也统计进来 
59             {
60                 c++;
61                 p = p->next;
62             }
63             break;
64          }
65          p = p->next;
66     }
67     printf("%d %d",ss,c);
68     p = head->next;
69     while(p&&p->score>=ss) 
70     {
71         printf("%d %d\n",p->bmh,p->score);
72         p = p->next;
73     }
74 }
75 
76 void destory(Node *head)
77 {
78     Node *p = head,*q;
79     while(p)
80     {
81         q = p;
82         p = p->next;
83         free(q);
84     }
85 }

字串计算

10101

0 2

01 2

1 3

10 2

101 2

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 typedef struct node
 6 { 
 7     char sub[100];
 8     int count;
 9     struct node* next;
10 }Node;
11 
12 void insert(Node *head,char sub[]);
13 void output(Node *head);
14 
15 int main (void)
16 {
17     char s[110],sub[100];
18     scanf("%s",s);
19     Node *head = (Node*)malloc(sizeof(Node));
20     head -> next = NULL;
21     int len = strlen(s);
22     for(int i = 1;i < len;i++)
23         for(int j = 0;j <= len-i;j++)
24         {
25             memset(sub,0,100);
26             strncpy(sub,s+j,i);//截取子串
27             insert(head,sub);//把子串插入到链表head中,已存在,只是计数加一
28         }
29     output(head);
30     
31     return 0;
32 }
33 
34 void insert(Node *head,char sub[])
35 {
36     Node *p = head->next;
37     Node *prep = head;
38     while(p != NULL)
39     {
40         if(strcmp(sub,p->sub) > 0)
41         {
42             prep = p;
43             p = p->next;
44         }
45         else break;
46      }
47      if(p&&strcmp(sub,p->sub) == 0) p->count++;//sub链表在子串中存在 
48      else//不存在,需要新插入一个节点 
49      {
50          Node *s = (Node*)malloc(sizeof(Node));
51          strcpy(s->sub,sub);
52          s->count = 1;
53          s->next = p;
54          prep->next = s;
55      }
56 }
57 
58 void output(Node *head)
59 {
60     Node *p = head->next;
61     while(p)
62     {
63         if(p->count>=2) printf("%s %d\n",p->sub,p->count);
64         p = p->next;
65     }
66 }

猜你喜欢

转载自www.cnblogs.com/FutureSience/p/12741038.html