HDU1716 排列2 组合数

排列2

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1438    Accepted Submission(s): 535


Problem Description
Ray又对数字的列产生了兴趣:
现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数。
 

Input
每组数据占一行,代表四张卡片上的数字(0<=数字<=9),如果四张卡片都是0,则输入结束。
 

Output
对每组卡片按从小到大的顺序输出所有能由这四张卡片组成的4位数,千位数字相同的在同一行,同一行中每个四位数间用空格分隔。
每组输出数据间空一行,最后一组数据后面没有空行。
 

Sample Input
1 2 3 4 1 1 2 3 0 1 2 3 0 0 0 0
 

Sample Output
1234 1243 1324 1342 1423 1432 2134 2143 2314 2341 2413 2431 3124 3142 3214 3241 3412 3421 4123 4132 4213 4231 4312 4321 1123 1132 1213 1231 1312 1321 2113 2131 2311 3112 3121 3211 1023 1032 1203 1230 1302 1320 2013 2031 2103 2130 2301 2310 3012 3021 3102 3120 3201 3210
 

  关于组合数组的字典序生成法是这样进行的:

  对于给定的一个数,首先从右边找到第一个相邻"逆序对",现假设下一个要找的数比这个数大,且中间没有一个数比前者大、比后者小。那么这个"逆序对"的定义是就是满足
num[ i ]< num[ i+ 1 ](假设这个整数是以数组的形式存储的),然后再重新从右边起找出第一个比那个"逆序对"的较小者 要大的数,交换他们,再将那个较小数下标后面的
子数组反转。。。。。   过程比较纠结啊,这里推荐一篇百度文档http://wenku.baidu.com/view/b45f2e6648d7c1c708a14539.html

// 字典序法生成组合数 

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

char e[10];

char hash[10005];  // hash 来过 

int argument_a( int len )
{
    int i;
    for( i= len- 2; i>= 0; --i ) // i位 直接与其前一位相比较 
    {               
        //printf( "%d\n", i );                 // 因此 i 的左界为 2
        if( e[i]< e[i+ 1]|| i== 0 )
        {
            //printf( "%d %d\n", e[i], e[i+ 1]  );
            break;
        }
    }
    return i;
}

int argument_b( int len, int parter )
{
    int i;
    for( i= len- 1; i>= 0; --i )
    {
        if( e[i]> parter|| i== 0 ) // 在连续的数组比较中的一种较为固定的控制,伪监视哨 
        {
            break; 
        }
    }
    return i;
}

bool is_end( int len )
{
    int i;
    for( i= len- 2; i>= 0; --i )
    {
        if( e[i]< e[i+ 1] )
        {
            return false;
        }
    }
    if( i== -1 ) // 说明一直遍历到了最前端 
    {
        return true;
    }
}

void swap( char &a, char &b )
{
    int temp= a;
    a= b;
    b= temp;
}

void reverse( int front, int rear )
{
    int i= front, j= rear- 1;
    while( i<= j )
    {
        swap( e[i], e[j] );
        ++i, --j;
    }
}

bool next( int len )
{
    if( is_end( len ) )
    {
        return false;
    } 
    else
    {
        int i= argument_a( strlen( e ) );
        int j= argument_b( strlen( e ), e[i] );
        swap( e[i], e[j] );
        reverse( i+ 1, 4 );
        return true;
    }
}

int cmp( const void *a, const void *b )
{
    return *( char * )a- *( char * )b;
}

int main(  )
{
    //freopen( "1.txt", "w", stdout );
    char a, b, flag= 0;
    while( scanf( "%c %c %c %c", &e[0], &e[1], &e[2], &e[3] )!= EOF )
    {
        getchar(  );
        e[4]= '\0';  // 使其成为一个合格的字符串 
        if( atoi( e )== 0 ) // 结束程序 
        {
            return 0;
        }
        if( flag )
        {
            printf( "\n" );   
        }
        flag= 1 ;
        memset( hash, 0, sizeof( hash ) );
        qsort( e, 4, sizeof( char ), cmp );
        //printf( "test= %s\n", e );
        hash[ atoi( e ) ]= 1;
        int tag= 0, last;
        if( atoi( e )/1000!= 0 )
        {
            tag= 1;
            last= atoi( e );
        }
        while( next( 4 ) )
        { 
            if( atoi( e )/1000!= 0 )
            {
                hash[ atoi( e ) ]= 1;
                if( !tag )
                {
                    last= atoi( e );
                    tag= 1;
                }
            }
        }
        //printf( "%d\n", last );
        for( int i= 1000; i<= 9999; ++i )
        {
            if( hash[i] )
            {
                if( i== last )
                {
                    printf( "%d", i );
                }
                else if( i/ 1000!= last/ 1000 )
                {
                    printf( "\n%d", i );
                } 
                else 
                {
                    printf( " %d", i );
                }
                last= i;
            }
        }
        printf( "\n" );
    }
    return 0; 
}

      看到题目便想到了以前看过的组合数生成算法,可惜当时是翻树状数组时无意间翻进去的,有点印象,但是记不大请了。。。。  所以感悟到练习是多么的重要。
原文地址:https://www.cnblogs.com/Lyush/p/2101952.html