《Programming Pearls》 column 2中问题二中解法 (不知道对不对哦)

/*
 * author: lx
 * date: 2011-09-16
 * brief: programming pearls column2
 */
#include <stdio.h>
#include <stdlib.h>


void
move_vector( char* b, int m, int n )
{
        int i = 0;
        int j;
        char temp;
        for ( ; i < n ; i++ )
        {
                for ( j = i; j < m; j += n )
                {
                        if ( j == i )
                                temp = *( b + j );
                        else
                                *( b + j - n ) = *( b + j) ;
                }
                printf( "temp is %c %d\n", temp, j );
                *( b + j - n ) = temp;
        }

        /*
         * this is especially.
         *
         */
        if ( m % n != 0 )
                move_vector( b + m - n , n, n - m%n );
        
}


int 
main()
{
        char a[8] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
        char b[12] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l' };

        move_vector( a, 8, 3 );

        int i;
        for ( i = 0; i < 8; i++ )
                printf( "%c\n", *( a + i ) );

        printf( "b is .............\n" );
        move_vector( b, 12, 3 );

        for ( i = 0; i < 12; i++ )
                printf( "%c\n", *( b + i ) );

        exit( 0 );
}

  

posted @ 2011-09-17 13:23  lxgeek  阅读(179)  评论(0编辑  收藏  举报