做洛谷看题解发现大佬用这个函数做的,感觉很强,学了一下,好像发现了新大陆,这是什么神仙函数啊!!!Orz
这个函数的作用就是把数组里的数按下一个字典序进行排序
举个例子吧,比如 a[3]={1,2,3};
排一次之后变{1,3,2}
再排一次变{2,1,3}
再排一次变{2,3,1}
写成程序就是这样的
#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){
int n=3,a[10],j=0,i;
for(i=0;i<3;i++){
scanf("%d",&a[i]);
}
next_permutation(a,a+n);
for(i=0;i<3;i++){
printf("%d ",a[i]);
}
return 0;
}
运行结果
然后我试了试字符串,发现也能排,代码差不多,直接贴结果了
然后我又试了试二维字符串,竟然还能排,我的天呐
#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){
int n,i;
char a[10][5];
scanf("%d",&n);
getchar();
for(i=0;i<n;i++){
scanf("%s",&a[i]);
}
printf("\n");
next_permutation(a,a+n);
for(i=0;i<n;i++){
printf("%s\n",a[i]);
}
return 0;
}
这是结果
就和写题最常用的一样,一位一位往下比较进行排序
还有一个prev_permutation函数,作用是上一个字典序排序,用法相同
好东西是要和大家分享的,试验完后立即写了这篇博客,太强了我服了