nyoj 19 擅长排列的小明

来源:互联网 发布:怎样在淘宝网上开店铺 编辑:程序博客网 时间:2024/05/21 07:56

擅长排列的小明

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
描述
小明十分聪明,而且十分擅长排列计算。比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想为难他,在这5个数字中选出几个数字让他继续全排列,那么你就错了,他同样的很擅长。现在需要你写一个程序来验证擅长排列的小明到底对不对。
输入
第一行输入整数N(1<N<10)表示多少组测试数据,
每组测试数据第一行两个整数 n m (1<n<9,0<m<=n)
输出
在1-n中选取m个字符进行全排列,按字典序全部输出,每种排列占一行,每组数据间不需分界。如样例
样例输入
23 14 2
样例输出
123121314212324313234414243


我们可以先求字符串s的全排列, 然后取s全排列的前y位组成的不同字符串(要检查所取子串是否与前面输出的子串相同,不同才输出)
例: x = 3, y = 1;
s = “123”
s的全排列为
123
132
213
231
312
321
而我们要取的为
1
2
3
#include<iostream>#include<algorithm>#include<string>using namespace std;int T, x, y;int main() {    cin >> T;    while(T--) {        string s, t, ts;        //字符串s放在while里面定义,如果在外面定义,应该每次循环时清空s        cin >> x >> y;        for(int i = 1; i <= x; i++) {            s += '0' + i;        }        t = s.substr(0, y);        //取s从第0位开始,由y位字符组成的子串        cout << t << endl;        while(next_permutation(s.begin(), s.end())) {            ts = s.substr(0, y);            if(ts != t) {                cout << ts << endl;                t = ts;            }        }    }    return 0;}



法二:
#include <stdio.h>  #include <string.h>  #include <stdlib.h>  #include <algorithm>  using namespace std;  int n,m,used[10];  char ans[11];  void dfs(int x,int num)  {      if(num==m-1) {          ans[m]='\0';          printf("%s\n",ans);          return;      }      int i;      for(i=1;i<=n;i++) {          if(!used[i]) {              used[i]=1;              ans[num+1]=i+'0';              dfs(i,num+1);              used[i]=0;          }      }  }  int main() {      int i,t;      scanf("%d",&t);      while(t--) {          scanf("%d%d",&n,&m);          for(i=1;i<=n;i++) {              memset(used,0,sizeof(used));              used[i]=1;              ans[0]=i+'0';              dfs(i,0);          }      }      return 0;  }          





附:求1.......x的升序全排列代码(1<= x && x <= 9)
#include<iostream>#include<algorithm>#include<string>using namespace std;string s;int T, x, y;int main() {    cin >> T;    while(T--) {        cin >> x >> y;        for(int i = 1; i <= x; i++) {            s += '0' + i;        }        cout << s << endl;        while(next_permutation(s.begin(), s.end())) {            cout << s << endl;        }    }}
C++中substr的用法:http://blog.csdn.net/no_retreats/article/details/7853066
C++中的全排列函数:http://www.cnblogs.com/xudong-bupt/p/3662986.html

升序全排列 next_permutation()
降续全排列 prev_permutation()

说明:next_permutation,重新排列范围内的元素[第一,最后一个)返回按照字典序排列的下一个值较大的组合。

返回值:如果有一个更高的排列,它重新排列元素,并返回true;如果这是不可能的(因为它已经在最大可能的排列),它按升序排列重新元素,并返回false。





0 0
原创粉丝点击