CC15. 牛牛的书
描述
牛牛正在买书,每本书都有名字和价格,牛牛想把书按照价格升序排序。输入描述
输出描述
把书名按照价格升序输出。示例1
输入:
3 TheNowcoder 100 Abook 20 BBook 300
输出:
Abook TheNowcoder BBook
C 解法, 执行用时: 2ms, 内存消耗: 300KB, 提交时间: 2022-05-21
#include<stdio.h> #include<malloc.h> #include<stdlib.h> typedef struct book{ char name[20]; int price; }Book; int compare(const void* a,const void* b){ return ((Book*)a)->price-((Book*)b)->price; } int main(){ int n; scanf("%d",&n); Book* book=(Book*)malloc(sizeof(Book)*n); for(int i=0;i<n;i++) scanf("%s%d",&(book+i)->name,&(book+i)->price); qsort(book,n,sizeof(book[0]),compare); for(int i=0;i<n;i++) printf("%s\n",(book+i)->name); return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 312KB, 提交时间: 2022-05-18
#include<stdio.h> #include<string.h> struct Book { char name[15]; int price; }; int main() { int n, price; scanf("%d", &n); struct Book arr[n]; for (int i = 0; i < n; ++i) { char name[15]; scanf("%s %d", name, &price); strcpy(arr[i].name, name); arr[i].price = price; } // 插入排序 for (int i = 1; i < n; ++i) { for (int j = i; j >= 0; --j) { if (arr[j].price < arr[j - 1].price) { char tmp_name[15]; strcpy(tmp_name, arr[j].name); int tmp_price = arr[j].price; strcpy(arr[j].name, arr[j-1].name); arr[j].price = arr[j-1].price; strcpy(arr[j-1].name, tmp_name); arr[j-1].price = tmp_price; } else break; } } for (int i = 0; i < n; ++i) { printf("%s\n", arr[i].name); } return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 320KB, 提交时间: 2022-08-06
#include<stdio.h> #include <string.h> struct book { char name[20]; int price; }; int main() { int n; scanf("%d",&n); struct book a[n]; for(int i = 0;i < n;i++) { scanf("%s %d\n",a[i].name,&a[i].price); } for(int i =1; i < n;i++) { for(int j=1;j < n;j++) { if(a[j].price < a[j-1].price) { int temp; char p[20]; temp=a[j].price; a[j].price = a[j-1].price; a[j-1].price= temp; strcpy( p,a[j].name); strcpy(a[j].name , a[j-1].name); strcpy( a[j-1].name,p); } } } for(int i =0;i <n;i++) { printf("%s\n",a[i].name); } }
C 解法, 执行用时: 2ms, 内存消耗: 320KB, 提交时间: 2022-07-26
#include <stdio.h> #include <stdlib.h> struct node { char book[100]; int data; struct node *next; }; struct node *create_book(int n) { struct node *pnew=NULL; struct node *head=NULL; struct node *tail=NULL; for(int i=0;i<n;i++) { pnew=(struct node *)malloc(sizeof(*pnew)); pnew->next=NULL; int x; scanf("%s", pnew->book); scanf("%d", &x); pnew->data=x; if(head==NULL)//无中生有 { head=pnew; tail=pnew; } else//从少到多 { if(pnew->data<head->data)//头插 { pnew->next=head; head=pnew; } else if(pnew->data>tail->data)//尾插 { tail->next=pnew; tail=pnew; } else//插中间 { struct node *p=head; struct node *pre=NULL; while(p) { if(pnew->data<=p->data) { pre->next=pnew; pnew->next=p; break; } pre=p; p=p->next; } } } } return head; } void printf_book(struct node *h)//输出 { struct node *p=h; while(p) { printf("%s\n",p->book); p=p->next; } } int main() { int n; scanf("%d",&n); struct node *p=create_book(n); printf_book(p); return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 320KB, 提交时间: 2022-06-13
#include<stdio.h> #include<string.h> typedef struct book { char name[30]; short price; }book; int main() { struct book arr[10]={0}; int n; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%s %d",&arr[i].name,&arr[i].price); } for(int i=0;i<n-1;i++) { for(int j=0;j<n-1-i;j++) { if(arr[j].price>arr[j+1].price) { book tmp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=tmp; } } } for(int i=0;i<n;i++) { printf("%s\n",arr[i].name); } return 0; }