50053. The Most Popular Author

難度:2/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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

typedef struct s_book{
char author_name[128];
char book_name[128];
} s_book;

typedef struct s_author{
char author_name[128];
int selling;
} s_author;

int n;
int author_cnt = 0;
s_book book[128];
s_author author[128];

int get_author_idx(char author_name[128]){
for(int i = 0; i < author_cnt; i++){
if(strcmp(author[i].author_name, author_name) == 0){
return i;
}
}
return -1;
}

int get_book_idx(char book_name[128]){
for(int i = 0; i < n; i++){
if(strcmp(book[i].book_name, book_name) == 0){
return i;
}
}
return -1;
}

int cmp(const void* a, const void* b){
s_author* sa = (s_author*)a;
s_author* sb = (s_author*)b;
if(sa->selling < sb->selling) return 1;
else if(sa->selling > sb->selling) return -1;
else return(strcmp(sa->author_name, sb->author_name));
}

int main(){
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%s %s", book[i].author_name, book[i].book_name);
int idx = get_author_idx(book[i].author_name);
if(idx == -1){
strcpy(author[author_cnt].author_name, book[i].author_name);
author[author_cnt].selling = 0;
author_cnt += 1;
}
}
for(int i = 0; i < n; i++){
char book_name[128];
int selling;
scanf("%s %d", book_name, &selling);
int book_idx = get_book_idx(book_name);
char* author_name = book[book_idx].author_name;
int author_idx = get_author_idx(author_name);
author[author_idx].selling += selling;
}
qsort(author, author_cnt, sizeof(s_author), cmp);
printf("%s %d\n", author[0].author_name, author[0].selling);

}


50053. The Most Popular Author
https://aaronlin1229.github.io/judgegirl_50053/
Author
Akizumi
Posted on
July 17, 2023
Licensed under