@@ -15,13 +15,13 @@ void leftShift1(char * arr, int n)
15
15
char tmpChar ;
16
16
int i , j ;
17
17
18
- if (tmpLen == 0 )
19
- return ;
18
+ if (tmpLen == 0 )
19
+ return ;
20
20
21
21
if (n >= 0 )
22
22
{
23
23
n %= tmpLen ;
24
- for (i = 0 ; i < n ; i ++ )
24
+ for (i = 0 ; i < n ; i ++ )
25
25
{
26
26
tmpChar = * arr ;
27
27
for (j = 0 ; j < tmpLen - 1 ; j ++ )
@@ -33,8 +33,8 @@ void leftShift1(char * arr, int n)
33
33
}
34
34
else
35
35
{
36
- n = - ((- n ) % tmpLen )
37
- for (i = 0 ; i < - n ; i ++ )
36
+ n = - ((- n ) % tmpLen );
37
+ for (i = 0 ; i < - n ; i ++ )
38
38
{
39
39
tmpChar = * (arr + tmpLen - 1 );
40
40
for (j = tmpLen - 1 ; j > 0 ; j -- )
@@ -54,28 +54,28 @@ void leftShift2(char * arr, int len, int n)
54
54
int p0 = 0 , p1 ;
55
55
char tmpChar ;
56
56
57
- if (n == 0 )
58
- return ;
57
+ if (n == 0 )
58
+ return ;
59
59
60
- if (n < 0 )
61
- n = len - (-1 * n ) % len ;
62
- n %= len ;
63
- if (n == 0 )
64
- return ;
60
+ if (n < 0 )
61
+ n = len - (-1 * n ) % len ;
62
+ n %= len ;
63
+ if (n == 0 )
64
+ return ;
65
65
66
- p1 = n ;
66
+ p1 = n ;
67
67
68
68
/* O(m - n - k) k is the last section*/
69
69
while (p1 + n - 1 < tmpLen )
70
70
{
71
- for (i = 0 ; i < n ; ++ i )
72
- {
73
- tmpChar = * (arr + p0 );
74
- * (arr + p0 ) = * (arr + p1 );
75
- * (arr + p1 ) = tmpChar ;
76
- p0 ++ ;
77
- p1 ++ ;
78
- }
71
+ for (i = 0 ; i < n ; ++ i )
72
+ {
73
+ tmpChar = * (arr + p0 );
74
+ * (arr + p0 ) = * (arr + p1 );
75
+ * (arr + p1 ) = tmpChar ;
76
+ p0 ++ ;
77
+ p1 ++ ;
78
+ }
79
79
}
80
80
/*
81
81
* not good O(k * (n + k)) k = tmpLen - p1
@@ -130,16 +130,16 @@ void leftShift4(char * arr, int len, int n)
130
130
int p0 = 0 , p1 ;
131
131
char tmpChar ;
132
132
133
- if (len == 0 )
134
- return ;
133
+ if (len == 0 )
134
+ return ;
135
135
136
- if (n < 0 )
137
- n = len - (-1 * n ) % len ;
138
- n %= len ;
139
- if (n == 0 )
140
- return ;
136
+ if (n < 0 )
137
+ n = len - (-1 * n ) % len ;
138
+ n %= len ;
139
+ if (n == 0 )
140
+ return ;
141
141
142
- p1 = n ;
142
+ p1 = n ;
143
143
144
144
/* O(m - n - k) k is the last section*/
145
145
while (p1 < tmpLen )
@@ -173,17 +173,17 @@ void myinvert(char * start, char * end)
173
173
//翻转法
174
174
void leftShift5 (char * arr , int len , int n )
175
175
{
176
- if (len == 0 )
177
- return ;
176
+ if (len == 0 )
177
+ return ;
178
178
179
- if (n < 0 )
180
- n = len - (-1 * n ) % len ;
181
- n %= len ;
179
+ if (n < 0 )
180
+ n = len - (-1 * n ) % len ;
181
+ n %= len ;
182
182
183
- if (n == 0 )
184
- return ;
183
+ if (n == 0 )
184
+ return ;
185
185
186
- myinvert (arr , arr + n - 1 );
186
+ myinvert (arr , arr + n - 1 );
187
187
myinvert (arr + n , arr + len - 1 );
188
188
myinvert (arr , arr + len - 1 );
189
189
}
@@ -202,21 +202,21 @@ int gcd(int m, int n)
202
202
//循环移位法
203
203
void leftShift6 (char * arr , int len , int n )
204
204
{
205
- int group , x , i , j ;
206
- char tmpChar ;
205
+ int group , x , i , j ;
206
+ char tmpChar ;
207
207
208
- if (len == 0 )
209
- return ;
208
+ if (len == 0 )
209
+ return ;
210
210
211
- if (n < 0 )
212
- n = len - (-1 * n ) % len ;
213
- n %= len ;
211
+ if (n < 0 )
212
+ n = len - (-1 * n ) % len ;
213
+ n %= len ;
214
214
215
- if (n == 0 )
216
- return ;
215
+ if (n == 0 )
216
+ return ;
217
217
218
- group = gcd (len , n );
219
- x = len / group ;
218
+ group = gcd (len , n );
219
+ x = len / group ;
220
220
221
221
for (i = 0 ; i < group ; i ++ )
222
222
{
@@ -228,72 +228,72 @@ void leftShift6(char * arr, int len, int n)
228
228
* (arr + (i + (j * n )) % len ) = tmpChar ;
229
229
}
230
230
}
231
+
231
232
int main ()
232
233
{
233
- char str [50 ];
234
- sprintf (str , "123456" );
235
- printf ("The origin str is :%s\tlen is :%zu\n" , str , strlen (str ));
236
- printf ("\n" );
237
- leftShift1 (str , 2 );
238
- printf ("The leftShift1 str is :%s\n" , str );
239
- leftShift1 (str , -2 );
240
- printf ("The leftShift1 str is :%s\n" , str );
241
- leftShift1 (str , 3 );
242
- printf ("The leftShift1 str is :%s\n" , str );
243
- leftShift1 (str , strlen (str ) - 3 );
244
- printf ("The leftShift1 str is :%s\n" , str );
245
- printf ("\n" );
246
-
247
- leftShift2 (str , strlen (str ), 3 );
248
- printf ("The leftShift2 str is :%s\n" , str );
249
- leftShift2 (str , strlen (str ), strlen (str ) - 3 );
250
- printf ("The leftShift2 str is :%s\n" , str );
251
- leftShift2 (str + 2 , strlen (str ) - 2 , 2 );
252
- printf ("The leftShift2 str is :%s\n" , str );
253
- leftShift2 (str + 2 , strlen (str ) - 2 , strlen (str ) - 2 - 2 );
254
- printf ("The leftShift2 str is :%s\n" , str );
255
- printf ("\n" );
256
-
257
-
258
- leftShift3 (str , strlen (str ), 3 );
259
- printf ("The leftShift3 str is :%s\n" , str );
260
- leftShift3 (str , strlen (str ), strlen (str ) - 3 );
261
- printf ("The leftShift3 str is :%s\n" , str );
262
- leftShift3 (str + 2 , strlen (str ) - 2 , 2 );
263
- printf ("The leftShift3 str is :%s\n" , str );
264
- leftShift3 (str + 2 , strlen (str ) - 2 , strlen (str ) - 2 - 2 );
265
- printf ("The leftShift3 str is :%s\n" , str );
266
- printf ("\n" );
267
-
268
- leftShift4 (str , strlen (str ), 3 );
269
- printf ("The leftShift4 str is :%s\n" , str );
270
- leftShift4 (str , strlen (str ), strlen (str ) - 3 );
271
- printf ("The leftShift4 str is :%s\n" , str );
272
- leftShift4 (str + 2 , strlen (str ) - 2 , 2 );
273
- printf ("The leftShift4 str is :%s\n" , str );
274
- leftShift4 (str + 2 , strlen (str ) - 2 , strlen (str ) - 2 - 2 );
275
- printf ("The leftShift4 str is :%s\n" , str );
276
- printf ("\n" );
277
-
278
- leftShift5 (str , strlen (str ), 3 );
279
- printf ("The leftShift5 str is :%s\n" , str );
280
- leftShift5 (str , strlen (str ), strlen (str ) - 3 );
281
- printf ("The leftShift5 str is :%s\n" , str );
282
- leftShift5 (str + 2 , strlen (str ) - 2 , 2 );
283
- printf ("The leftShift5 str is :%s\n" , str );
284
- leftShift5 (str + 2 , strlen (str ) - 2 , strlen (str ) - 2 - 2 );
285
- printf ("The leftShift5 str is :%s\n" , str );
286
- printf ("\n" );
287
-
288
- leftShift6 (str , strlen (str ), 3 );
289
- printf ("The leftShift6 str is :%s\n" , str );
290
- leftShift6 (str , strlen (str ), strlen (str ) - 3 );
291
- printf ("The leftShift6 str is :%s\n" , str );
292
- leftShift6 (str + 2 , strlen (str ) - 2 , 2 );
293
- printf ("The leftShift6 str is :%s\n" , str );
294
- leftShift6 (str + 2 , strlen (str ) - 2 , strlen (str ) - 2 - 2 );
295
- printf ("The leftShift6 str is :%s\n" , str );
296
- printf ("\n" );
297
-
298
- return 0 ;
234
+ char str [50 ];
235
+ sprintf (str , "123456" );
236
+ printf ("The origin str is :%s\tlen is :%zu\n" , str , strlen (str ));
237
+ printf ("\n" );
238
+ leftShift1 (str , 2 );
239
+ printf ("The leftShift1 str is :%s\n" , str );
240
+ leftShift1 (str , -2 );
241
+ printf ("The leftShift1 str is :%s\n" , str );
242
+ leftShift1 (str , 3 );
243
+ printf ("The leftShift1 str is :%s\n" , str );
244
+ leftShift1 (str , strlen (str ) - 3 );
245
+ printf ("The leftShift1 str is :%s\n" , str );
246
+ printf ("\n" );
247
+
248
+ leftShift2 (str , strlen (str ), 3 );
249
+ printf ("The leftShift2 str is :%s\n" , str );
250
+ leftShift2 (str , strlen (str ), strlen (str ) - 3 );
251
+ printf ("The leftShift2 str is :%s\n" , str );
252
+ leftShift2 (str + 2 , strlen (str ) - 2 , 2 );
253
+ printf ("The leftShift2 str is :%s\n" , str );
254
+ leftShift2 (str + 2 , strlen (str ) - 2 , strlen (str ) - 2 - 2 );
255
+ printf ("The leftShift2 str is :%s\n" , str );
256
+ printf ("\n" );
257
+
258
+ leftShift3 (str , strlen (str ), 3 );
259
+ printf ("The leftShift3 str is :%s\n" , str );
260
+ leftShift3 (str , strlen (str ), strlen (str ) - 3 );
261
+ printf ("The leftShift3 str is :%s\n" , str );
262
+ leftShift3 (str + 2 , strlen (str ) - 2 , 2 );
263
+ printf ("The leftShift3 str is :%s\n" , str );
264
+ leftShift3 (str + 2 , strlen (str ) - 2 , strlen (str ) - 2 - 2 );
265
+ printf ("The leftShift3 str is :%s\n" , str );
266
+ printf ("\n" );
267
+
268
+ leftShift4 (str , strlen (str ), 3 );
269
+ printf ("The leftShift4 str is :%s\n" , str );
270
+ leftShift4 (str , strlen (str ), strlen (str ) - 3 );
271
+ printf ("The leftShift4 str is :%s\n" , str );
272
+ leftShift4 (str + 2 , strlen (str ) - 2 , 2 );
273
+ printf ("The leftShift4 str is :%s\n" , str );
274
+ leftShift4 (str + 2 , strlen (str ) - 2 , strlen (str ) - 2 - 2 );
275
+ printf ("The leftShift4 str is :%s\n" , str );
276
+ printf ("\n" );
277
+
278
+ leftShift5 (str , strlen (str ), 3 );
279
+ printf ("The leftShift5 str is :%s\n" , str );
280
+ leftShift5 (str , strlen (str ), strlen (str ) - 3 );
281
+ printf ("The leftShift5 str is :%s\n" , str );
282
+ leftShift5 (str + 2 , strlen (str ) - 2 , 2 );
283
+ printf ("The leftShift5 str is :%s\n" , str );
284
+ leftShift5 (str + 2 , strlen (str ) - 2 , strlen (str ) - 2 - 2 );
285
+ printf ("The leftShift5 str is :%s\n" , str );
286
+ printf ("\n" );
287
+
288
+ leftShift6 (str , strlen (str ), 3 );
289
+ printf ("The leftShift6 str is :%s\n" , str );
290
+ leftShift6 (str , strlen (str ), strlen (str ) - 3 );
291
+ printf ("The leftShift6 str is :%s\n" , str );
292
+ leftShift6 (str + 2 , strlen (str ) - 2 , 2 );
293
+ printf ("The leftShift6 str is :%s\n" , str );
294
+ leftShift6 (str + 2 , strlen (str ) - 2 , strlen (str ) - 2 - 2 );
295
+ printf ("The leftShift6 str is :%s\n" , str );
296
+ printf ("\n" );
297
+
298
+ return 0 ;
299
299
}
0 commit comments