习题2.8 输出全排列
2017-09-20 17:22
519 查看
习题2.8 输出全排列(20 分)
请编写程序输出前n个正整数的全排列(n<10),并通过9个测试用例(即n从1到9)观察n逐步增大时程序的运行时间。
输入格式:
输入给出正整数n(<10)。
输出格式:
输出1到n的全排列。每种排列占一行,数字间无空格。排列的输出顺序为字典序,即序列a1,a2,⋯,an排在序列b1,b2,⋯,bn之前,如果存在k使得a1=b1,⋯,ak=bk 并且 ak+1<bk+1。
困扰了好几天的一道题,用循环递归来做吧,会有重复现象。
用交换法来做吧,又会出现结果非字典序的情况。
好在发现了C++竟然有这么好用的库函数。
激动!!!
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[10];
int main()
{
int i,j,n,m;
scanf("%d",&n);
for(i=0;i<n;i++)
a[i]=i+1;
do
{
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
printf("\n");
}while(next_permutation(a,a+n));
puts("");
return 0;
}
1) int 类型的next_permutation int main() { int a[3]; a[0]=1;a[1]=2;a[2]=3; do { cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl; } while (next_permutation(a,a+3)); //参数3指的是要进行排列的长度 //如果存在a之后的排列,就返回true。如果a是最后一个排列没有后继,返回false,每执行一次,a就变成它的后继 } 输出: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 如果改成 while(next_permutation(a,a+2)); 则输出: 1 2 3 2 1 3 只对前两个元素进行字典排序 显然,如果改成 while(next_permutation(a,a+1)); 则只输出:1 2 3 若排列本来就是最大的了没有后继,则next_permutation执行后,会对排列进行字典升序排序,相当于循环 int list[3]={3,2,1}; next_permutation(list,list+3); cout<<list[0]<<" "<<list[1]<<" "<<list[2]<<endl; //输出: 1 2 3 (2) char 类型的next_permutation int main() { char ch[205]; cin >> ch; sort(ch, ch + strlen(ch) ); //该语句对输入的数组进行字典升序排序。如输入9874563102 cout<<ch; 将输出0123456789,这样就能输出全排列了 char *first = ch; char *last = ch + strlen(ch); do { cout<< ch << endl; }while(next_permutation(first, last)); return 0; } //这样就不必事先知道ch的大小了,是把整个ch字符串全都进行排序 //若采用 while(next_permutation(ch,ch+5)); 如果只输入1562,就会产生错误,因为ch中第五个元素指向未知 //若要整个字符串进行排序,参数5指的是数组的长度,不含结束符 (3) string 类型的next_permutation int main() { string line; while(cin>>line&&line!="#") { if(next_permutation(line.begin(),line.end())) //从当前输入位置开始 cout<<line<<endl; else cout<<"Nosuccesor\n"; } } int main() { string line; while(cin>>line&&line!="#") { sort(line.begin(),line.end());//全排列 cout<<line<<endl; while(next_permutation(line.begin(),line.end())) cout<<line<<endl; } } next_permutation 自定义比较函数 #include<iostream> //poj 1256 Anagram #include<string> #include<algorithm> using namespace std; int cmp(char a,char b) //'A'<'a'<'B'<'b'<...<'Z'<'z'. { if(tolower(a)!=tolower(b)) return tolower(a)<tolower(b); else return a<b; } int main() { char ch[20]; int n; cin>>n; while(n--) { scanf("%s",ch); sort(ch,ch+strlen(ch),cmp); do { printf("%s\n",ch); }while(next_permutation(ch,ch+strlen(ch),cmp)); } return 0; }
相关文章推荐
- 习题2.8 输出全排列(20 分)
- 输出全排列的算法
- 非递归,按序输出集合的全排列
- 输出全排列 (next_permutation)
- 循环结构_习题:输出Fibonacci数列前20项
- YTU 2435: C++ 习题 输出日期时间--友元函数
- 《APUE.3E》习题4.6编写自己的cp(l)程序,它复制包含空洞的文件,但不将字节0包含到输出文件中去
- 输出一个数n的全排列
- SICP 习题 (2.8) 解题总结:区间的减法
- 1058 习题4-11 4个整数从小到大输出
- 输出一个字符串的全排列
- 有1,2,2,3,4,5六个数,要求输出他们组合的全排列,4不能在第三位,5和3不能相邻
- 习题:可以输入年纪,如果你的年纪大于18,则输出你需要对你的年纪负责。
- n全排列输出和 n个数的组合(数字范围a~b)
- 第二章习题——求圆的各种公式,考察输出控制iomanip的使用
- 递归输出全排列和全组合
- Problem E: C语言习题 学生成绩输入和输出
- YTU 2436: C++ 习题 输出日期时间--友元类
- 习题 1.5 请参照本章例题,编写一个C程序,输出以下信息:*****Very good!*****
- 习题 4.6 有一个函数:y=x (x<1) y=2x-1 (1<=x<10) y=3x-11 (x>=10) 写程序,输入x的值,输出y相应的值。