zoukankan      html  css  js  c++  java
  • 洛谷 P1088 火星人 (全排列)

    直接调用next_permutation即可,向前的话可以调用prev_permutation

    #include<cstdio>
    #include<cctype>
    #include<algorithm>
    #define REP(i, a, b) for(int i = (a); i < (b); i++)
    #define _for(i, a, b) for(int i = (a); i <= (b); i++)
    using namespace std;
    
    const int MAXN = 11234;
    int a[MAXN], n, m;
    void read(int& x)
    {
    	int f = 1; x = 0; char ch = getchar();
    	while(!isdigit(ch)) { if(ch == '-1') f = -1; ch = getchar(); }
    	while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
    	x *= f;
    }
    
    int main()
    {
    	read(n); read(m);
    	REP(i, 0, n) read(a[i]);
    	while(m--) next_permutation(a, a + n);
    	printf("%d", a[0]); REP(i, 1, n) printf(" %d", a[i]);
    	puts("");
    	return 0;
    }
    

    还可以不用STL做,原理见https://blog.csdn.net/HyJoker/article/details/50899362

    #include<cstdio>
    #include<cctype>
    #include<algorithm>
    #define REP(i, a, b) for(int i = (a); i < (b); i++)
    #define _for(i, a, b) for(int i = (a); i <= (b); i++)
    using namespace std;
    
    const int MAXN = 11234;
    int a[MAXN], n, m;
    void read(int& x)
    {
    	int f = 1; x = 0; char ch = getchar();
    	while(!isdigit(ch)) { if(ch == '-1') f = -1; ch = getchar(); }
    	while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
    	x *= f;
    }
    
    void next()
    {
    	int j, k;
    	for(j = n - 2; j >= 0 && a[j] > a[j+1]; j--);
    	for(k = n - 1; k >= 0 && a[k] < a[j]; k--);
    	swap(a[k], a[j]);
    	reverse(a + j + 1, a + n);
    }
    
    int main()
    {
    	read(n); read(m);
    	REP(i, 0, n) read(a[i]);
    	while(m--) next();
    	printf("%d", a[0]); REP(i, 1, n) printf(" %d", a[i]);
    	puts("");
    	return 0;
    }
    
  • 相关阅读:
    虚拟机下unbuntu上网
    Ubuntu 用户切换和管理
    Brew程序模拟器上运行出现中文乱码
    struts2+spring+hibernate实例
    ubuntu ip设置
    JavaScript常用总结
    C++动态创建二维数组和清空cin缓冲
    🍖类的组合
    🍖鸭子类型
    🍖类的多态与多态性
  • 原文地址:https://www.cnblogs.com/sugewud/p/9819354.html
Copyright © 2011-2022 走看看