백준 1181 : 단어 정렬

이번에도 역시 구조체를 응용한 퀵 정렬을 사용하였다. 문제 자체의 난이도가 높지는 않았다

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct arr {
	char word[51];
	int length;
} words;

int compare(const void *a, const void *b) {
	words str1 = *(words *)a;
	words str2 = *(words *)b;
	
	if(str1.length > str2.length)
		return 1;
	else if(str1.length < str2.length)
		return -1;
	else 
		return strcmp(str1.word, str2.word);
}

int solve() {
	int N;
	scanf("%d", &N);
	
	words res[N];
	for(int i=0;i<N;i++) {
		scanf("%s", &res[i].word);
		res[i].length = strlen(res[i].word);
	}
	
	qsort(res, N, sizeof(res[0]), compare);

	for(int i=0;i<N;i++) {
		if(strcmp(res[i-1].word, res[i].word) != 0)
			printf("%s\n", res[i].word);
	}

}

int main() {
	solve();
	return 0; 
}

 

'백준 (C99) > 정렬 (完)' 카테고리의 다른 글

백준 18870 : 좌표 압축  (0) 2022.02.18
백준 10814 : 나이순 정렬  (0) 2022.02.06
백준 11651 : 좌표 정렬하기 2  (0) 2022.02.06
백준 11650 : 좌표 정렬하기  (0) 2022.02.06
백준 1427 : 소트인사이드  (0) 2022.02.05