50090. Count Pointers

難度: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
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
#include "count.h"

typedef struct s_pair{
int* address;
int count;
} s_pair;

int cmp(const void* a, const void* b){
s_pair* pa = (s_pair*)a;
s_pair* pb = (s_pair*)b;

int na = *(pa->address);
int nb = *(pb->address);
// printf("cmping %d %d\n", na, nb);

if(pa->count != pb->count) return pa->count - pb->count;
else return (na) - (nb);
}

void count(int **p[]){
s_pair pair[512] = {0};
int cnt = 0;

for(int i = 0; p[i] != NULL; i++){
int* a = *(p[i]);

int j;
for(j = 0; j < cnt; j++){
if(pair[j].address == a){
pair[j].count++;
break;
}
}
if(j == cnt){
pair[cnt].address = a;
pair[cnt].count = 1;
cnt++;
}
}
qsort(pair, cnt, sizeof(s_pair), cmp);

for(int i = 0; i < cnt; i++){
printf("%d %d\n", *(pair[i].address), pair[i].count);
}
}


50090. Count Pointers
https://aaronlin1229.github.io/judgegirl_50090/
Author
Akizumi
Posted on
July 17, 2023
Licensed under