• unique && stl的全排列


    stl的全排列:

    看代码。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<string>
     5 #include<cstring>
     6 using namespace std;
     7 int n,a[12];
     8 int main()
     9 {
    10     scanf("%d",&n);
    11     for(int i=1;i<=n;++i) a[i]=i;
    12     while(next_permutation(a+1,a+n+1))//下一个全排列函数 
    13     {
    14         for(int i=1;i<=n;++i)
    15          cout<<a[i]<<" ";
    16         cout<<endl;
    17     }//prev_permutation(a+1,a+n)  <-上一个全排列函数 
    18     return 0;
    19 }

    =============================================================================、

    unique() 去重函数:
    STL中unique的函数 unique的功能是去除相邻的重复元素(只保留一个),

    还有一个容易忽视的特性是它并不真正把重复的元素删除。

    使用时头文件要加#include<iostream>。

    因为unique去除的是相邻的重复元素,所以一般用之前都会要排一下序

    具体用法如下:

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 using namespace std;
     7 const int MAXN=8010;
     8 int a[MAXN];
     9 int main()
    10 {
    11     int N;
    12     while(scanf("%d",&N)!=EOF)
    13    {
    14         for(int i=1;i<=N;i++)scanf("%d",&a[i]);
    15         sort(a+1,a+N+1);
    16         int k=unique(a+1,a+N+1)-a;
    17         printf("k=%d
    ",k);
    18         for(int i=1;i<=N;i++)
    19        printf("%d ",a[i]),puts("");
    20     }
    21     return 0;
    22 }

    (๑′ᴗ‵๑)I Lᵒᵛᵉᵧₒᵤ❤。

  • 相关阅读:
    当今世界最为经典的十大算法投票进行时
    HDU_1203 I NEED A OFFER!
    POJ_2352 Stars(树状数组)
    HDU_1231 最大连续子序列
    POJ_3264 Balanced Lineup(线段树练手题)
    【转】休息几分种,学几个bash快捷键
    HDU_1013 Digital Roots
    HDU_1381 Crazy Search
    POJ_2528 Mayor's posters(线段树+离散化)
    zoj_1610 Count tht Color
  • 原文地址:https://www.cnblogs.com/adelalove/p/8492527.html
Copyright © 2020-2023  润新知