解题思路:
注意事项:注意字符串相等的比较,注意计算不同数组的长度,注意存储的是float型的变量。
参考代码:
#include<stdio.h>
#include<string.h>
#include<malloc.h>
typedef struct _Node{
char s[8];
float high;
struct _Node *next;
}Node,*Linklist;
Linklist list_init();
Linklist linklist_creat_h(int n);
Linklist linklist_creat_t(int n);
int linklist_bianli(Linklist L,float P_hign[],int m);
void paixu(float P_hign[],int shuzu_length);
int main()
{
Node *L1;
int n,k,length_female,length_male;
float *female1,*male1;
scanf("%d",&n);
female1 = (float*)malloc(sizeof(float)*n);
male1 = (float*)malloc(sizeof(float)*n);
L1 = linklist_creat_t(n);
length_female = linklist_bianli(L1,female1,1);
length_male = linklist_bianli(L1,male1,2);
paixu(female1,length_female);
paixu(male1,length_male);
//
for(k=0;k<length_female;k++)
{
printf("%0.2f ",female1[k]);
}
for(k=length_male-1;k>=0;k--)
{
printf("%0.2f ",male1[k]);
}
printf("\n");
return 0;
}
Linklist list_init()
{
Node *L;
L = (Node*)malloc(sizeof(Node));
if(L==NULL)
{
printf("申请空间失败");
}
L->next = NULL;
return L;
}
Linklist linklist_creat_h(int n)
{
Node *L;
int i;
L = (Node*)malloc(sizeof(Node));
L->next = NULL;
for(i = 0;i<n;i++)
{
Node *p;
p = (Node*)malloc(sizeof(Node));
scanf("%s %f",&p->s,&p->high);
p->next = L->next;
L->next = p;
}
return L;
}
Linklist linklist_creat_t(int n)
{
Node *L,*r;
int i;
L = (Node*)malloc(sizeof(Node));
L->next = NULL;
r = L;
for(i=0;i<n;i++)
{
Node *p;
p = (Node*)malloc(sizeof(Node));
scanf("%s %f",&p->s,&p->high);
r->next = p;
r = p;
//printf("%s %0.2f\n",p->s,p->high);
}
r->next = NULL;
//printf("list leng = %d\n",i);
//printf("--------------one-----------------\n");
return L;
}
int linklist_bianli(Linklist L,float P_hign[],int m)
{
Node *p;
int j=0,length=0;
float mid;
p = L->next;
if(m==1)
{
while(p!=NULL)
{
if(strcmp("female",p->s))
{
//printf("%0.2f ",p->high);
mid = p->high;
P_hign[j] = mid;
//printf("%0.2f\n",P_hign[j]);
length++;
j++;
}
p = p->next;
}
}
else
{
while(p!=NULL)
{
if(strcmp("male",p->s))
{
//printf("%0.2f ",p->high);
mid = p->high;
P_hign[j] = mid;
//printf("%0.2f\n",P_hign[j]);
length++;
j++;
}
p = p->next;
}
}
free(p);
return length;
// printf("--------------two-----------------\n");
}
void paixu(float P_hign[],int shuzu_length)
{
int i=0,j;
float mid;
for(i=0;i<shuzu_length;i++)
{
for(j=i+1;j<shuzu_length;j++)
{
if(P_hign[i]>P_hign[j])
{
mid = P_hign[i];
P_hign[i] = P_hign[j];
P_hign[j] = mid;
}
}
}
//printf("%d\n",strlen(P_hign)/sizeof(float));
// printf("--------------three-----------------\n");
}
0.0分
2 人评分
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:481 |
1013题解浏览:596 |
循环入门练习5 (C语言代码)浏览:907 |
简单的a+b (C语言代码)浏览:857 |
C语言程序设计教程(第三版)课后习题7.4 (C语言代码)浏览:476 |
小O的乘积 (C++代码)浏览:796 |
明明的随机数 (C语言代码)浏览:965 |
统计立方数 (C语言代码)浏览:890 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:456 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:476 |