|
发表于 2022-6-1 14:14:21
|
显示全部楼层
本楼为最佳答案
 - typedef struct STUDENT
- {
- char name[32];
- int id;
- float lesson;
- }stdt,*pst;
- void stsort(pst st,int n)
- {
- int *id=(int*)malloc(sizeof(int)*n);
- for (int i = 0; i < n; i++)
- {
- id[i]=i;
- }
- for(int i=0;i<n-1;i++)
- {
- for(int j=n-1;j>i;j--)
- {
- if(st[id[j]].lesson<st[id[j-1]].lesson)
- {
- int tmp=id[j];
- id[j]=id[j-1];
- id[j-1]=tmp;
- }
- }
- printf("%s:%f\n",st[id[i]].name,st[id[i]].lesson);
- }
- printf("%s:%f\n",st[id[n-1]].name,st[id[n-1]].lesson);
- free(id);
- }
- void orderfind(pst st,int n,char *name)
- {
- for(;n>=0;n--)
- {
- int j=0;
- while(name[j]&&(name[j]==st[n].name[j++]));
- if('\0'==name[j]&&'\0'==st[n].name[j])
- {
- printf("%f\n",st[n].lesson);
- break;
- }
- }
- if(-1==n)printf("no body\n");
- }
- int halffind(pst st,int id,int l,int r)
- {
- /*if(st[(l+r)/2].id==id)return (l+r)/2;
- if(l>=r)return -1;
- int ret =-1;
- if((ret=ishalffind(st,id,0,r/2)>-1))return ret;
- if((ret=ishalffind(st,id,r/2+1,r)>-1))return ret;
- return ret;*/
- if (l>r)
- {
- return -1;
- }
- int half=(l+r)/2;
- if(id==st[half].id)return half;
- if(id>st[half].id)return halffind(st,id,half+1,r);
- if (id<st[half].id)return halffind(st,id,0,half);
-
- }
- int insertstt(pst st,int *n)
- {
- if(*n>=20)return 0;
- scanf("%s%d%f",st[*n].name,&st[*n].id,&st[*n].lesson);
- for (int i = *n; i>0; i--)
- {
- if (st[i-1].id>st[i].id)
- {
- stdt tmp=st[i];
- st[i]=st[i-1];
- st[i-1]=tmp;
- }
- else
- break;
- }
- (*n)++;
- return 1;
- }
- int main()
- {
- stdt st[20]={0};
- int n=0,id=0;
- for (int i = 0; i < 20; i++)
- {
- if (0==insertstt(st,&n))break;
- }
- stsort(st,20);
- char nm[32]={0};
- scanf("%s",nm);
- orderfind(st,20,nm);
- scanf("%d",&id);
- int ret=halffind(st,id,0,20-1);
- if(ret>=0)printf("%f",st[ret].lesson);
- else
- ("no body\n");
- return 0;
- }
复制代码- aaa 102 1
- bbb 111 2
- ccc 342 3
- ddd 210 4
- eee 423 5
- fff 649 6
- ggg 666 7
- hhh 555 8
- iii 612 9
- jjj 456 11
- kkk 234 10
- lll 98 12
- mmm 674 13
- nnn 416 14
- ooo 222 15
- ppp 555 16
- qqq 999 17
- rrr 877 18
- sss 681 19
- ttt 539 20
- aaa:1.000000
- bbb:2.000000
- ccc:3.000000
- ddd:4.000000
- eee:5.000000
- fff:6.000000
- ggg:7.000000
- hhh:8.000000
- iii:9.000000
- kkk:10.000000
- jjj:11.000000
- lll:12.000000
- mmm:13.000000
- nnn:14.000000
- ooo:15.000000
- ppp:16.000000
- qqq:17.000000
- rrr:18.000000
- sss:19.000000
- ttt:20.000000
- ttt
- 20.000000
- 555
- 8.000000
复制代码
|
|