题解:三连击

算法
地址:https://www.luogu.com.cn/problem/P1008
Published

October 12, 2018

CautionContent note — Known errors · 明显错误

三连击文章把排列编号相关概念写成“泰勒展开”,这是明显术语错误。

  • 排列序号相关概念通常是康托展开,不是泰勒展开。
  • 文中 perv_permutation 拼写错误,应为 prev_permutation

题解:三连击

1.题目

地址:https://www.luogu.com.cn/problem/P1008 借这个题熟悉下next_permutation()的用法
这个函数主要就是把英语记住其他就和sort差不多的用 如果已经到了最后一个序列就会返回false。 next_ per mu ta tion 每次执行一次就会将原有的序列打乱成下一个序列

2.注意

面意思,就是下一个排列,那么考虑一个问题,如果当前排列不是第一个排列呢(关于第几个排列的问题如果不懂的请百度下泰勒展开),就会有他的孪生兄弟:prev_permutation()用法一样,只是求出的是上一个序列。一个序列的第一个排列就是他的升序排列
幸好这个题给的数据都是排过序的。所以说对于无序的序列来讲,可以先next_permutation()+perv_permutation();或者先排个序然后再next_permutation();就可以求出全部的排列了。

3.代码:

#include <iostream>
#include <cstdio>
#include <algorithm> 
using namespace std;
int a[9]={1,2,3,4,5,6,7,8,9};
int main(){
    do{
        int next_a=a[0]*100+a[1]*10+a[2];
        int next_b=a[3]*100+a[4]*10+a[5];
        int next_c=a[6]*100+a[7]*10+a[8];
        if(next_a*3==next_c && next_a*2==next_b)
        cout<<next_a<<" "<<next_b<<" "<<next_c<<endl;
    }while(next_permutation(a,a+9));
    return 0;
}