50062. Merge Book Information
難度:3/5
題目看清楚。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "book.h"
int cmp(const void* a, const void* b){
BookInfo* b_a = (BookInfo*)a;
BookInfo* b_b = (BookInfo*)b;
int s = strcmp(b_a->book_title, b_b->book_title);
if(s != 0) return s;
else{
return b_a->date > b_b->date;
}
}
BookInfoList merge(int N, int M, BookAuthor authorArr[], BookSelling sellingArr[]){
BookInfoList ans;
ans.num = 0;
for(int i = 0; i < M; i++){
int j;
for(j = 0; j < N; j++){
if(strcmp(authorArr[j].book_title, sellingArr[i].book_title) == 0){
break;
}
}
if(j == N) continue;
strcpy(ans.books[ans.num].book_title, sellingArr[i].book_title);
ans.books[ans.num].selling = sellingArr[i].selling;
ans.books[ans.num].date = sellingArr[i].date;
strcpy(ans.books[ans.num].author, authorArr[j].author);
ans.num++;
}
qsort(ans.books, ans.num, sizeof(BookInfo), cmp);
return ans;
}