c++ 实现全排序

news/2024/7/4 0:54:02 标签: c++, permutation, class, im, c
cle class="tags" href="/tags/CLASS.html" title=class>class="baidu_pl">
cle_content" class="tags" href="/tags/CLASS.html" title=class>class="article_content clearfix">
content_views" class="tags" href="/tags/CLASS.html" title=class>class="htmledit_views"> 载C++代码(3)全排列 
<code class="tags" href="/tags/CLASS.html" title=class>class="language-cpp">#include <assert.h>
#include <iostream>
#include <algorithm>
using namespace std;

template<typename T>
void Perm(T* pT, int n, int m)
{
    if (n == m)
    {
        for (int i=0; i<m; i++)
            cout << pT[i] << " ";
        cout << endl;
        return;
    }
    else
    {
        for (int i=m; i<n; i++)
        {
            swap(pT[m], pT[i]);
            Perm(pT, n, m+1);
            swap(pT[m], pT[i]);
        }
    }
}



class="tags" href="/tags/CLASS.html" title=class>class Permutation
{
public:
    enum {MAX_SIZE = 10};
    Permutation(int count)
    {
        assert(0 < count && count < MAX_SIZE);
        m_nCount = (char)count;
        for (char i=0; i<m_nCount; i++)
            m_Indexs[i] = i;
    }
    
public:
    bool ToNext();
    
    char operator[] (int n)const
    {
        assert(0 <= n && n <= m_nCount);
        return m_Indexs[n];
    }
    
private:
    char m_Indexs[MAX_SIZE];
    char m_nCount;
};

bool Permutation::ToNext()
{
    char nReverse = m_nCount-1;
    while (nReverse > 0 && m_Indexs[nReverse]<m_Indexs[nReverse-1])    
        nReverse--;    // 寻找第一个要交换的元素
    if (nReverse == 0)    // 找不到࿰c;表示全排已经完成
        return false;
    
    char nSwap = m_nCount - 1;
    while (m_Indexs[nSwap] < m_Indexs[nReverse-1])
        nSwap--;    // 寻找第二个元素
    swap(m_Indexs[nReverse-1], m_Indexs[nSwap]);    // 开始交换
    reverse(m_Indexs+nReverse, m_Indexs+m_nCount);    // 逆顺
    return true;
}


int main()
{
    const int N = 3;
    Permutation perm(N);
    const char* sTests[N] = {"aaa", "bbb", "ccc"};
    do
    {
        for (int i=0; i<N; i++)
            cout << sTests[perm[i]] << " ";
        cout << endl;
    }while(perm.ToNext());
    return 0;
}

code>

cle>

http://www.niftyadmin.cn/n/520377.html

相关文章

c++ 实现部分排序,待改进

C代码&#xff08;4&#xff09;排列与组合 #include <assert.h>#include <iostream>#include <algorithm>using namespace std;template<typename T>void Perm(T* pT, int n, int m){if (n m){for (int i0; i<m; i)cout << pT[i] << …

linux(cygwin) 获取进程号

#include <stdio.h> #include <unistd.h> int main ( ) {printf ("The process ID is %d\n", (int) getpid ()) ; //printf ("The parent process ID is %d\n", (int) getppid ()) ; //return 0 ; }

ctrl-c,ctrl-z,ctrl-d区别

linux下&#xff1a;ctrl-c 发送 SIGINT 信号给前台进程组中的所有进程。常用于终止正在运行的程序。ctrl-z 发送 SIGTSTP 信号给前台进程组中的所有进程&#xff0c;常用于挂起一个进程。ctrl-d 不是发送信号&#xff0c;而是表示一个特殊的二进制值&#xff0c;表示 EOF。ctr…

种子填充算法在验证码识别中的的应用

种子填充算法在验证码识别中的的应用 需要模拟精灵v7.15 下载&#xff1a;http://www.yhhe.net/bbs/dispbbs.asp?boardID4&ID2851&page1 模拟精灵识别验证码的能用是强大的&#xff0c;一个函数即可以去除杂色杂点&#xff0c;但是有时候验证码中有大量的干扰线&a…

Docker修改名称

docker 容器&#xff08;服务&#xff09;重命名只要一个命令就可以&#xff1a; docker rename 原容器名 新容器名 如&#xff1a;

OCR验证码识别

最近&#xff0c;想做一个刷票的程序&#xff0c;有 验证码 &#xff0c;好在验证码不是很复杂&#xff0c;所以就想着能不能识别。开始想用svm来分类&#xff0c;但是不知道怎么回事&#xff0c;识别率达不到要求&#xff0c;可能中间的某个环节没有做好。听说神经网络也很好&…

简单、直观的实现优于复杂、难懂的实现,最近开发扑克识别过程的总结

最近开发了款通用的扑克识别程序&#xff0c;本文谈下心得。最开始是准备使用Shape Context或Zernike矩来识别的&#xff0c;写出了Shape Context&#xff0c;发现识别率达不到理想状态。接着准备用Zernike矩实现&#xff0c;查找文献发现&#xff0c;Zernike矩虽然理论上对旋转…

如何修改Docker已运行实例的端口映射

如何修改Docker已运行实例的端口映射 Docker的端口映射&#xff0c;往往出现在两个阶段需要处理&#xff1a; 1、是在docker启动前就已经确定好&#xff0c;哪个docker实例映射哪个端口&#xff08;往往这个情况比较&#xff0c;需要提前做规划&#xff09;。 2、在docker运…