Q

백준 10811

백준 10811번 문제


C code

배열 관점

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
#include <stdio.h>

int swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}

int main(void)
{
int n, m, i, j;
int arr[100] = {0,};

scanf("%d %d", &n, &m);

if((n < 1 && n > 100) && (m < 1 && m > 100))
return 0;

for(int i = 0; i < n; i++)
arr[i] = i+1;

for(int a = 0; a < m; a++)
{
scanf("%d %d", &i, &j);

if((i < 1 && i > n) && (j < 1 && j > n))
return 0;

for(int b = i; b < j; b++)
{
for(int d = i; d < (j + i - b); d++)
swap(&arr[d-1], &arr[d]);
}
}

for(int i = 0; i < n; i++)
printf("%d ", arr[i]);

return 0;
}

사용자 관점

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
#include <stdio.h>

int swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}

int main(void)
{
int n, m, i, j;
int arr[100] = {0,};

scanf("%d %d", &n, &m);

if((n < 1 && n > 100) && (m < 1 && m > 100))
return 0;

for(int i = 1; i <= n; i++)
arr[i] = i;

for(int a = 0; a < m; a++)
{
scanf("%d %d", &i, &j);

if((i < 1 && i > n) && (j < 1 && j > n))
return 0;

for(int b = i; b < j; b++)
{
for(int c = i; c < (j + i - b); c++)
swap(&arr[c], &arr[c+1]);
}
}

for(int i = 1; i <= n; i++)
printf("%d ", arr[i]);

return 0;
}

과정 출력

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
#include <stdio.h>

int swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}

int main(void)
{
int n, m, i, j;
int arr[100] = {0,};

scanf("%d %d", &n, &m);

if((n < 1 && n > 100) && (m < 1 && m > 100))
return 0;

for(int i = 1; i <= n; i++)
arr[i] = i;

for(int i = 1; i <= n; i++)
printf("%d ", arr[i]);
printf("\n\n");

for(int a = 0; a < m; a++)
{
scanf("%d %d", &i, &j);

if((i < 1 && i > n) && (j < 1 && j > n))
return 0;

for(int b = i; b < j; b++)
{
for(int c = i; c < (j + i - b); c++)
{
swap(&arr[c], &arr[c+1]);

for(int i = 1; i <= n; i++)
printf("%d ", arr[i]);
printf("\n");
}
printf("\n");
}
printf("\n");
}

for(int i = 1; i <= n; i++)
printf("%d ", arr[i]);

return 0;
}

Note

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
arr = 1 2 3 4 5

// i, j = 1, 2
2 1 3 4 5

// i, j = 3, 4
2 1 4 3 5

//i, j = 1, 4
b for문 : 1 < 4
c for문 : -> 1 2 4 3 5
c for문 : -> 1 4 2 3 5
c for문 : -> 1 4 3 2 5

b for문 : 2 < 4
c for문-> 4 1 3 2 5
c for문-> 4 3 1 2 5

b for문 : 3 < 4
c for문-> 3 4 1 2 5

// i, j = 2, 2
3 4 1 2 5