VioletaBabel
10989번: 수 정렬하기 3 본문
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int N, *num, *copynum, *temp, *copytemp, count[10];
scanf("%d", &N);
num = (int *)malloc(sizeof(int)*N);
copynum = (int *)malloc(sizeof(int)*N);
copytemp = (int *)malloc(sizeof(int)*N);
temp = (int *)malloc(sizeof(int)*N);
for (int i = 0; i < N; ++i)
scanf("%d", &num[i]);
memcpy(copynum, num, (sizeof(int)*N));
for (int i = 0; i < 5; ++i)
{
memset(count, 0, sizeof(count));
for (int j = 0; j < N; ++j)
++count[(copynum[j] % 10)];
for (int j = 0; j < 9; ++j)
count[j + 1] += count[j];
for (int j = N - 1, k; j > -1; --j)
{
k = copynum[j] % 10;
temp[(--(count[k]))] = num[j];
copytemp[count[k]] = copynum[j];
}
for (int j = 0; j < N; ++j)
{
num[j] = temp[j];
copynum[j] = (copytemp[j] / 10);
}
}
for (int i = 0; i < N; ++i)
printf("%d\n", num[i]);
free(num);
free(copynum);
free(copytemp);
free(temp);
}
=====radix정렬로 해보려고 했지만 런타임 에러. 메모리 부족으로 보인다.
=====그래서 에라 모르겠다하고 그냥 counting sort를 말도안되게 변형해서 사용
#include <stdio.h>
#include <string.h>
int main()
{
int N, num[10001], in, max = 0;
memset(num, 0, sizeof(num));
scanf("%d", &N);
for (int i = 0; i < N; ++i)
{
scanf("%d", &in);
++num[in];
max = (max > in) ? max : in;
}
for (int i = 0; i < max+1; ++i)
for (; num[i] > 0; --num[i])
printf("%d\n", i);
}
'백준 > 백준-C' 카테고리의 다른 글
1427번: 소트인사이드 (0) | 2017.05.05 |
---|---|
2108번: 통계학 (0) | 2017.05.05 |
2751번: 수 정렬하기 2 (0) | 2017.05.02 |
2750번: 수 정렬하기 (0) | 2017.05.02 |
11866번: 조세퍼스 문제 (0) | 2017.04.25 |