-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockSwap.c
58 lines (57 loc) · 1.22 KB
/
BlockSwap.c
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
#include "header.h"
void BS(int Length, int D, char* Str) //BlockSwap
{
int A = 0, Z = Length - 1; //맨처음 맨끝 좌표
int L = D, R = Length - D; //좌 우 길이
if (D == 0)
return 0;
while (L != R) //좌 우 길이가 같지않을때까지 반복
{
if (L < R)
{
Swap(A, Z, L, Str);
Z -= L;
R -= L;
}
else
{
Swap(A, Z, R, Str);
A += R;
L -= R;
}
}
Swap(A, Z, R, Str); //좌 우 길이가 같을때 마지막으로 swap해주고 마침
}
void mBS(int Length, int D, char* Str) //BlockSwap
{
int A = 0, Z = Length - 1; //맨처음 맨끝 좌표
int L = Length - D, R = D; //좌 우 길이
if (D == 0)
return 0;
while (L != R) //좌 우 길이가 같지않을때까지 반복
{
if (L < R)
{
Swap(A, Z, L, Str);
Z -= L;
R -= L;
}
else
{
Swap(A, Z, R, Str);
A += R;
L -= R;
}
}
Swap(A, Z, R, Str); //좌 우 길이가 같을때 마지막으로 swap해주고 마침
}
void Swap(int a, int z, int sD, char* Str) //Str[a]~Str[z]중에서 앞뒤에서 각각 sD만큼 바꿔주는 함수
{
char temp = NULL;
for (int i = 0; i < sD; i++)
{
temp = Str[a + i];
Str[a + i] = Str[z - sD + i + 1];
Str[z - sD + i + 1] = temp;
}
}