第一行输入一个整数
代表学生人数。
第二行输入一个整数
代表排序方式,其中,
表示按成绩降序,
表示按成绩升序。
此后
行,第
行依次输入:
一个长度为
、由大小写字母构成的字符串
代表第
个学生的姓名;
一个整数
代表这个学生的成绩。
根据输入的排序方式,按照成绩升序或降序输出所有学生的姓名和成绩。对于每一名学生,新起一行。输出学生的姓名和成绩,用空格分隔。
3 0 fang 90 yang 50 ning 70
fang 90 ning 70 yang 50
在这个样例中,
,因此按成绩降序排序。
4 1 fang 90 yang 50 ning 70 yang 70
yang 50 ning 70 yang 70 fang 90
在这个样例中,
,因此按成绩升序排序。
本题已于下方时间节点更新,请注意题解时效性:
1. 2025-05-16 更新题面。
2. 2025-01-09 更新题面。
#include <stdio.h>
/* 居然还需要是个稳定排序,用快速排序的可以放弃了,有用例会没法AC */
/* 思路:利用结构体来进行存储,排序规则自定义排序函数,调用即可。 */
typedef struct info {
char str[20];
int score;
} info;
/* 降序 */
void method0(info arr[], int left, int right) {
if (left >= right) {
return;
}
int i = left;
int j = right;
info pivot = arr[left];
/* 快速排序 */
while (i < j) {
if (i < j && arr[j].score <= pivot.score)
{
j--;
}
arr[i] = arr[j];
if (i < j && arr[i].score >= pivot.score)
{
i++;
}
arr[j] = arr[i];
}
arr[i] = pivot;
method0(arr, left, i - 1);
method0(arr, i + 1, right);
}
/* 升序 */
void method1(info arr[], int left, int right) {
if (left >= right) {
return;
}
int i = left;
int j = right;
info pivot = arr[left];
/* 快速排序 */
while (i < j) {
if (i < j && arr[j].score >= pivot.score)
{
j--;
}
arr[i] = arr[j];
if (i < j && arr[i].score <= pivot.score)
{
i++;
}
arr[j] = arr[i];
}
arr[i] = pivot;
method1(arr, left, i - 1);
method1(arr, i + 1, right);
}
int main() {
int num, method;
scanf("%d", &num);
scanf("%d", &method);
info list[num];
for (int i = 0; i < num ; i++) {
scanf("%s", list[i].str);
scanf("%d", &list[i].score);
}
#if 0
for (int i = 0; i < num; i++) {
printf("%s ", list[i].str);
printf("%d\r\n", list[i].score);
}
#endif
/* 排序 */
if (method == 0) {
method0(list, 0, num-1);
} else if (method == 1) {
method1(list, 0, num-1);
}
for (int i = 0; i < num; i++) {
printf("%s ", list[i].str);
printf("%d\r\n", list[i].score);
}
}
#include <stdio.h>
#include <search.h>
typedef struct student {
char name[10];
int score;
} stu;
stu sc[200];
int cmp1(const void* e1, const void* e2) {
return ((stu*)e1)->score - ((stu*)e2)->score;
}
int cmp2(const void* e1, const void* e2) {
return ((stu*)e2)->score - ((stu*)e1)->score;
}
int main() {
int n;
int tag;
scanf("%d", &n);
scanf("%d", &tag);
for (int i = 0; i < n; ++i) {
scanf("%s %d", sc[i].name, &sc[i].score);
}
if (tag == 1) {
qsort(sc, n, sizeof(stu), cmp1);
for (int i = 0; i < n; ++i) {
printf("%s %d\n", sc[i].name, sc[i].score);
}
} else {
qsort(sc, n, sizeof(stu), cmp2);
for (int i = 0; i < n; ++i) {
printf("%s %d\n", sc[i].name, sc[i].score);
}
}
return 0;
} #include<stdio.h>
#include <stdlib.h>
// 结构体快速排序
struct stu{
char a[100];
int score;
}class[1000];
int cmp1(const void*e1,const void*e2){
return ((struct stu*)e1)->score-((struct stu*)e2)->score;
}
int cmp2(const void*e1,const void*e2){
return ((struct stu*)e2)->score-((struct stu*)e1)->score;
}
int main(){
int n=0;
int b=0;
scanf("%d\n",&n);
scanf("%d\n",&b);
for(int i = 0;i<n;i++){
scanf("%s %d",&class[i].a,&class[i].score);
}
if(b==1){
qsort(class,n,sizeof(class[0]),cmp1);
for(int i =0;i<n;i++){
printf("%s %d\n",class[i].a,class[i].score);
}
}
if(b==0){
qsort(class,n,sizeof(class[0]),cmp2);
for(int i =0;i<n;i++){
printf("%s %d\n",class[i].a,class[i].score);
}
}
return 0;
} 在分数不超过100的情况下用hash
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct score_info {
char *name;
struct score_info *next;
};
struct score_info *hash[101] = {NULL};
int main()
{
int num, cmp_func, score, end, step;
char str[20] = {0};
struct score_info *tmp, *cur;
scanf("%d", &num);
scanf("%d", &cmp_func);
while (scanf("%s", str) != EOF && scanf("%d", &score) != EOF) {
tmp = (struct score_info*)malloc(sizeof(struct score_info));
tmp->name = strdup(str);
tmp->next = NULL;
if (!hash[score]) {
hash[score] = tmp;
} else {
for (cur = hash[score]; cur->next; cur = cur->next)
;
cur->next = tmp;
}
}
num = cmp_func == 0 ? 100 : 0;
end = 100 - num;
step = cmp_func == 0 ? -1 : 1;
while (num != (end + step)) {
for (cur = hash[num]; cur; cur = cur->next)
printf("%s %d\n", cur->name, num);
num += step;
}
return 0;
}
#include <stdio.h>
#define N 200
typedef struct student
{
char name[20];
int score;
}student;
void ascend_sort(student *p,int n)
{
int i,j;
student temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(p[j].score>p[j+1].score)
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
}
void descend_sort(student *p,int n)
{
int i,j;
student temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(p[j].score<p[j+1].score)
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
}
int main()
{
student stu[N];
int i,n,flag;
scanf("%d%d",&n,&flag);
for(i=0;i<n;i++)
{
scanf("%s%d",stu[i].name,&stu[i].score);
}
//bubble_sort
if(flag) //升序
ascend_sort(stu,n);
else //降序
descend_sort(stu,n);
for(i=0;i<n;i++)
{
printf("%s %d\n",stu[i].name,stu[i].score);
}
return 0;
} #include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef enum{OK=0,ERROR}Status;
typedef struct NameScore
{
char *name;
int score;
struct NameScore *next;
}NameScore,*NameScoreP;
NameScoreP InitLink(int up_down)
{
NameScoreP head=NULL;
head = (NameScoreP)malloc(sizeof(NameScore));
head->next=NULL;
if(up_down==1)
head->score=-1;
else
head->score=101;
return head;
}
Status InsertLink(NameScoreP head,NameScoreP e,int up_down)
{
NameScoreP q=NULL,p=NULL;
NameScoreP qt=NULL;
q=head;
qt = (NameScoreP)malloc(sizeof(NameScore));
memcpy(qt, e, sizeof(NameScore));
if(up_down==1)
{
while(e->score >= q->score && q!=NULL)
{
p=q;
q=q->next;
}
qt->next=q;
p->next=qt;
}
else
{
while(e->score <= q->score && q!=NULL)
{
p=q;
q=q->next;
}
qt->next=q;
p->next=qt;
}
return OK;
}
int main()
{
int i,j,k;
char name[20];
int stu_count,up_down,namelength;
NameScoreP NSLink=NULL;
NameScoreP q1=NULL,q2=NULL;
scanf("%d",&stu_count);
scanf("%d",&up_down);
NSLink = InitLink(up_down);
q1 = (NameScoreP)malloc(sizeof(NameScore));
for(i=0;i<stu_count;i++)
{
scanf("%s",name);
namelength = strlen(name);
q1->name = (char *)malloc(sizeof(char)*namelength);
memcpy(q1->name, name,namelength);
scanf("%d",&q1->score);
InsertLink(NSLink,q1,up_down);
}
free(q1);
for(i=0;i<stu_count;i++)
{
q1=NSLink->next;
printf("%s %d\n",q1->name,q1->score);
NSLink->next=q1->next;
free(q1->name);
free(q1);
}
free(NSLink);
return 0;
}
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct{
char name[20];
int score;
}Student_Def;
typedef enum{
decrease = 0,
increase
}Rank_Def;
int main()
{
int num = 0;
Rank_Def rank;
char temp1[20] = {0};
int temp2 = 0;
int j = 0;
while(scanf("%d", &num) != EOF)
{
Student_Def student_DEC[1000] = {0};
Student_Def student_INC[1000] = {0};
/*输入*/
scanf("%d", &rank);
for(int i=0; i <num; i++)
{
scanf("%s", student_INC[i].name);
scanf("%d", &student_INC[i].score);
/*拷贝*/
strcpy(student_DEC[i].name, student_INC[i].name);
student_DEC[i].score = student_INC[i].score;
}
/*插入排序,稳定,升序*/
if(rank == increase)
{
for(int i=1; i < num; i++)
{
temp2 = student_INC[i].score;
strcpy(temp1, student_INC[i].name);
for(j=i-1; j >= 0; j--)
{
if(temp2 < student_INC[j].score)
{
student_INC[j+1].score = student_INC[j].score;
strcpy(student_INC[j+1].name, student_INC[j].name);
}
else break;
}
student_INC[j+1].score = temp2;
strcpy(student_INC[j+1].name, temp1);
}
/*输出*/
for(int i=0; i < num; i++)
{
printf("%s ", student_INC[i].name);
printf("%d", student_INC[i].score);
printf("\n");
}
}
else /*冒泡排序,稳定,降序*/
{
for(int i=1; i < num; i++)
{
for(j=0; j < num-i; j++)
{
if(student_DEC[j].score < student_DEC[j+1].score)
{
temp2 = student_DEC[j].score;
student_DEC[j].score = student_DEC[j+1].score;
student_DEC[j+1].score = temp2;
strcpy(temp1, student_DEC[j].name);
strcpy(student_DEC[j].name, student_DEC[j+1].name);
strcpy(student_DEC[j+1].name, temp1);
}
}
}
/*输出*/
for(int i=0; i < num; i++)
{
printf("%s ", student_DEC[i].name);
printf("%d", student_DEC[i].score);
printf("\n");
}
}
}
} #include <stdio.h>
#include <string.h>
void maopao(int *chengji,char xingming[][50],int len){
//void maopao(int *arr,char xm[][50],int len){
int arr[len];
for(int i=0;i<len;i++) arr[i]=chengji[i];
char xm[len][50];
for(int i=0;i<len;i++) strcpy(xm[i], xingming[i]);
for(int i=0;i<len;i++){
for(int j=0;j<len-i;j++){
if(arr[j]>arr[j+1]){
int a=0;
a=arr[j];
arr[j]=arr[j+1];
arr[j+1]=a;
char str[50]="";
strcpy(str, xm[j]);
strcpy(xm[j], xm[j+1]);
strcpy(xm[j+1], str);
}
}
}
for(int i=0;i<len;i++) strcpy(xingming[i],xm[i] );
for(int i=0;i<len;i++) chengji[i]=arr[i];
}
int main(){
char flage=1;
while(flage){
char next=0;
int renshu=0,shunxu=0;
scanf("%d%d",&renshu,&shunxu);
if(renshu<=0) break;
char xingming[renshu][50];
memset(xingming, '\0', renshu);
int chengji[renshu];
memset(chengji, 0, renshu);
for(int i=0;i<renshu;i++){
memset(xingming[i], '\0', 50);
scanf("%s",xingming[i]);
scanf("%d",&chengji[i]);
}
maopao(chengji, xingming, renshu);
if(shunxu==1){
for(int i=0;i<renshu;i++){
printf("%s %d\n",xingming[i],chengji[i]);
}
}else{
for(int i=renshu-1;i>=0;i--){
if(chengji[i]==chengji[i-1]){
int j=0;
j=i;
do{
j--;
}while(chengji[j]==chengji[j-1]);
int z=0;
z=j;
for(j;j<=i;j++){
printf("%s %d\n",xingming[j],chengji[j]);
}
i=z;
}else printf("%s %d\n",xingming[i],chengji[i]);
}
}
scanf("%c",&next);
if(next=='\n') flage=1;
else flage=0;
}
return 0;
} #include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct list{
char name[100];
int val;
struct list *next;
}List;
void listsort(List *head,int num,int flag)
{
int i,j;
char nametemp[100];
int valtemp;
List *p=head;
if(flag==1)
{
for(i=0;i<num-1;i++)
{
p=head;
for(j=0;j<num-1-i;j++)
{
if(p->val>p->next->val)
{
strcpy(nametemp,p->name);
strcpy(p->name,p->next->name);
strcpy(p->next->name,nametemp);
valtemp=p->val;
p->val=p->next->val;
p->next->val=valtemp;
}
p=p->next;
}
}
}
else
{
for(i=0;i<num-1;i++)
{
p=head;
for(j=0;j<num-1-i;j++)
{
if(p->val<p->next->val)
{
strcpy(nametemp,p->name);
strcpy(p->name,p->next->name);
strcpy(p->next->name,nametemp);
valtemp=p->val;
p->val=p->next->val;
p->next->val=valtemp;
}
p=p->next;
}
}
}
}
int main ()
{
int num,flag;
int i,j;
while(scanf("%d",&num)!=EOF)
{
scanf("%d",&flag);
char name[100][100];
int chengji[100];
List *ret=NULL;
List *last=ret;
for(i=0;i<num;i++)
{
scanf("%s %d",name[i],&chengji[i]);
List *temp=malloc(sizeof(List));
strcpy(temp->name,name[i]);
temp->val=chengji[i];
temp->next=NULL;
if(ret==NULL)
{
ret=temp;
last=temp;
}
else
{
last->next=temp;
last=temp;
}
}
listsort(ret,num,flag);
List *p=ret;
while(p!=NULL)
{
printf("%s %d\n",p->name,p->val);
p=p->next;
}
}
return 0;
}