Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1759560
  • 博文数量: 198
  • 博客积分: 4088
  • 博客等级: 上校
  • 技术积分: 2391
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-15 16:29
个人简介

游戏开发,系统架构; 博客迁移到:http://www.jianshu.com/u/3ac0504b3b8c

文章分类

全部博文(198)

文章存档

2017年(1)

2016年(12)

2015年(1)

2014年(3)

2013年(13)

2012年(18)

2011年(150)

分类: C/C++

2012-01-06 16:51:47

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;

  4. void swap(char &a, char &b)
  5. {
  6.     char temp;
  7.     temp = a;
  8.     a = b;
  9.     b = temp;
  10. }

  11. void perm(char* a, int k, int n, int &count)
  12. {
  13.     if( k < n )
  14.     {
  15.         for(int i = k; i < n ; ++i)
  16.         {
  17.             swap(a[k], a[i]);
  18.             perm(a, k + 1, n, count);
  19.             swap(a[k], a[i]);
  20.         }
  21.     }
  22.     else
  23.     {
  24.         count++;
  25.         for(int i = 0; i < n; ++i)
  26.         {
  27.             cout << a[i];
  28.         }

  29.         cout << endl;

  30.     }
  31. }

  32. int main()
  33. {
  34.     string str;
  35.     while(cin >> str)
  36.     {
  37.         int count = 0;
  38.         perm((char*)str.c_str(), 0, str.size(), count);

  39.         cout << "total: " << count << endl;
  40.     }

  41.     return 0;
  42. }
阅读(1618) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~