• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 21,  comments - 9,  trackbacks - 0
            這是一個(gè)全排列算法的C++模板, 與STL中泛型算法next_permutation的功能是一樣的。

            template < typename T >
            bool _next_permutation( T *first, T *last );
            #define N 8
            int main(int argc, char* argv[])
            {
                    int i;
                    int y = 1;//序號(hào)
                    char a[N];
                    for ( i=0; i<N; i++ )
                    {
                            a[i] = i + 1 + 64 + 32;
                    }

                    long t0 = time( NULL );
                    do
                    {
                            cout << y << " ---> ";
                            for ( i = 0; i<N; i++ ) cout << a[i];
                            cout << endl;

                            y++;
                    }while( _next_permutation( &a[0], &a[N] ) );
                    long t1 = time( NULL ) - t0;
                    cout << t1 << endl;//當(dāng)N為8時(shí)耗時(shí)130秒,與泛型算法next_permutation用時(shí)一樣

                    return 0;
            }

            template < typename T >
            bool _next_permutation( T *first, T *last )
            {
                    int i;
                    int j;
                    int x = -1;
                    int rang = last - first;
                   
                    for ( i=0; i<rang-1; i++ )
                    {
                            if ( *( first+i ) <= *( first+i+1 ) )
                            {
                                    x = i;
                            }
                    }

                    if ( x != -1 )
                    {
                            for ( i=x; i<rang; i++ )
                            {
                                    if ( *( first+x ) <= *( first+i ) )
                                    {
                                            j = i;
                                    }
                            }

                            _swap( *( first+x ), *( first+j ) );

                            for ( i=x+1; i<rang; i++ )
                            {
                                    if ( i != rang + x - i )
                                    {
                                            int nSwap = rang + x - i;
                                            _swap( *( first+i ), *( first+ ( rang+x-i ) ) );
                                    }
                                    if ( ( i + 1 ) * 2 > rang + x )
                                    {
                                            break;
                                    }
                            }
                    }

                    if ( -1 == x ) return false;
                    else return true;
            }

            template < typename T >
            void _swap( T &a, T &b )
            {
                    a = a + b;
                    b = a - b;
                    a = a - b;
            }

            posted on 2010-08-20 10:51 崔佳星 閱讀(1987) 評(píng)論(2)  編輯 收藏 引用

            FeedBack:
            # re: 全排列算法(Using c++ template):
            2010-08-23 14:18 | 路過(guò)
            交換(_swap)的方法不好,對(duì)整型沒(méi)有什么問(wèn)題,其它類型就....比如浮點(diǎn)數(shù)等等  回復(fù)  更多評(píng)論
              
            # re: 全排列算法(Using c++ template):
            2012-04-06 10:38 | 崔佳星
            C++里的浮點(diǎn)數(shù)比較還是很精確的。6位以內(nèi)的小數(shù)都能準(zhǔn)確比較。  回復(fù)  更多評(píng)論
              

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            <2010年8月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            热久久国产精品| 久久久亚洲欧洲日产国码二区 | 无码AV波多野结衣久久| 亚洲精品乱码久久久久久中文字幕| 久久精品国产亚洲av水果派| 日韩一区二区三区视频久久| 久久久一本精品99久久精品88 | 99久久无色码中文字幕人妻| 精品综合久久久久久97超人| 久久亚洲精品国产亚洲老地址| 狠狠色丁香久久婷婷综合五月| 亚洲v国产v天堂a无码久久| 久久国产精品久久久| 一本一本久久aa综合精品| 久久av免费天堂小草播放| 国产综合久久久久久鬼色| 久久无码AV一区二区三区| 久久996热精品xxxx| avtt天堂网久久精品| 久久综合综合久久综合| 久久久精品人妻一区二区三区蜜桃| 99久久综合国产精品二区| 国产精品久久久福利| 久久精品亚洲一区二区三区浴池| 青春久久| 思思久久99热只有频精品66| 久久久人妻精品无码一区| 国产高清国内精品福利99久久| …久久精品99久久香蕉国产| 亚洲国产一成人久久精品| 亚洲综合伊人久久大杳蕉| 99久久免费国产精品特黄| 狠狠色丁香婷婷久久综合| 伊人久久成人成综合网222| 久久只有这精品99| 久久人人爽人人爽人人av东京热| 精品久久久久久无码不卡| 久久久久久伊人高潮影院| 日产精品久久久一区二区| 久久99国产乱子伦精品免费| av无码久久久久久不卡网站|