fork(14) download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void swap( char* a, char* b )
  5. {
  6. char t = *a;
  7. *a = *b;
  8. *b = t;
  9. }
  10.  
  11. void reverse( char* str, int low, int high )
  12. {
  13. while( low < high )
  14. {
  15. swap( &str[low], &str[high] );
  16. ++low;
  17. --high;
  18. }
  19. }
  20.  
  21. void cycleLeaderIteration( char* str, int shift, int len )
  22. {
  23. int i, j, item, element;
  24.  
  25. for( i = 1; i < len; i *= 3 )
  26. {
  27. j = i;
  28.  
  29. item = str[j + shift];
  30. do
  31. {
  32. // odd index
  33. if( j & 1 )
  34. j = len / 2 + j / 2;
  35. // even index
  36. else
  37. j /= 2;
  38.  
  39. // keep the back-up of element at new position
  40. element = str[j + shift];
  41.  
  42. str[j + shift] = item;
  43.  
  44. item = element;
  45. } while( j != i );
  46. }
  47. }
  48.  
  49. void moveNumberToSecondHalf( char* str )
  50. {
  51. int i, j, k, lenFirst, lenRemaining, shift;
  52.  
  53. lenRemaining = strlen( str );
  54. shift = 0;
  55.  
  56. while( lenRemaining )
  57. {
  58. k = 0;
  59. while( pow( 3, k ) + 1 <= lenRemaining )
  60. k++;
  61.  
  62. lenFirst = pow( 3, k - 1 ) + 1;
  63. lenRemaining -= lenFirst;
  64.  
  65. cycleLeaderIteration( str, shift, lenFirst );
  66.  
  67.  
  68. reverse( str, shift / 2, shift - 1 );
  69.  
  70. reverse( str, shift, shift + lenFirst / 2 - 1 );
  71.  
  72. reverse( str, shift / 2, shift + lenFirst / 2 - 1 );
  73.  
  74. shift += lenFirst;
  75. }
  76. }
  77.  
  78. void Test()
  79. {
  80. int len, i, alph, digit ;
  81. char str[100];
  82.  
  83. for( len = 2; len <= 52; len += 2 )
  84. {
  85. for( i = 0; i < len; i++ )
  86. {
  87. if( i & 1 )
  88. str[i] = i / 2 % 9 + 1 + '0';
  89. else
  90. str[i] = i / 2 + 'a';
  91. }
  92. str[ len ] = '\0';
  93.  
  94. printf( "Test case#%d\n", len / 2 );
  95. printf( "%s\n", str );
  96.  
  97. moveNumberToSecondHalf( str );
  98.  
  99. printf( "%s\n\n\n", str );
  100. }
  101. }
  102.  
  103. int main()
  104. {
  105. Test();
  106.  
  107. return 0;
  108. }
Success #stdin #stdout 0.01s 1676KB
stdin
Standard input is empty
stdout
Test case#1
a1
a1


Test case#2
a1b2
ab12


Test case#3
a1b2c3
abc123


Test case#4
a1b2c3d4
abcd1234


Test case#5
a1b2c3d4e5
abcde12345


Test case#6
a1b2c3d4e5f6
abcdef123456


Test case#7
a1b2c3d4e5f6g7
abcdefg1234567


Test case#8
a1b2c3d4e5f6g7h8
abcdefgh12345678


Test case#9
a1b2c3d4e5f6g7h8i9
abcdefghi123456789


Test case#10
a1b2c3d4e5f6g7h8i9j1
abcdefghij1234567891


Test case#11
a1b2c3d4e5f6g7h8i9j1k2
abcdefghijk12345678912


Test case#12
a1b2c3d4e5f6g7h8i9j1k2l3
abcdefghijkl123456789123


Test case#13
a1b2c3d4e5f6g7h8i9j1k2l3m4
abcdefghijklm1234567891234


Test case#14
a1b2c3d4e5f6g7h8i9j1k2l3m4n5
abcdefghijklmn12345678912345


Test case#15
a1b2c3d4e5f6g7h8i9j1k2l3m4n5o6
abcdefghijklmno123456789123456


Test case#16
a1b2c3d4e5f6g7h8i9j1k2l3m4n5o6p7
abcdefghijklmnop1234567891234567


Test case#17
a1b2c3d4e5f6g7h8i9j1k2l3m4n5o6p7q8
abcdefghijklmnopq12345678912345678


Test case#18
a1b2c3d4e5f6g7h8i9j1k2l3m4n5o6p7q8r9
abcdefghijklmnopqr123456789123456789


Test case#19
a1b2c3d4e5f6g7h8i9j1k2l3m4n5o6p7q8r9s1
abcdefghijklmnopqrs1234567891234567891


Test case#20
a1b2c3d4e5f6g7h8i9j1k2l3m4n5o6p7q8r9s1t2
abcdefghijklmnopqrst12345678912345678912


Test case#21
a1b2c3d4e5f6g7h8i9j1k2l3m4n5o6p7q8r9s1t2u3
abcdefghijklmnopqrstu123456789123456789123


Test case#22
a1b2c3d4e5f6g7h8i9j1k2l3m4n5o6p7q8r9s1t2u3v4
abcdefghijklmnopqrstuv1234567891234567891234


Test case#23
a1b2c3d4e5f6g7h8i9j1k2l3m4n5o6p7q8r9s1t2u3v4w5
abcdefghijklmnopqrstuvw12345678912345678912345


Test case#24
a1b2c3d4e5f6g7h8i9j1k2l3m4n5o6p7q8r9s1t2u3v4w5x6
abcdefghijklmnopqrstuvwx123456789123456789123456


Test case#25
a1b2c3d4e5f6g7h8i9j1k2l3m4n5o6p7q8r9s1t2u3v4w5x6y7
abcdefghijklmnopqrstuvwxy1234567891234567891234567


Test case#26
a1b2c3d4e5f6g7h8i9j1k2l3m4n5o6p7q8r9s1t2u3v4w5x6y7z8
abcdefghijklmnopqrstuvwxyz12345678912345678912345678