STL中next_permutation的使用

11 篇文章 0 订阅
本文介绍了C++中next_permutation函数的应用,通过示例展示了如何进行字符串的原地全排列。此外,还提供了一个面试题案例,讨论了如何使用next_permutation进行数位重排并判断是否能生成整除关系的新排列。
摘要由CSDN通过智能技术生成

next_permutation主要用于原地全排列的情况。
cppreference案例:

#include <algorithm>
#include <string>
#include <iostream>
 
int main()
{
    std::string s = "aba";
    std::sort(s.begin(), s.end());
    do {
        std::cout << s << '\n';
    } while(std::next_permutation(s.begin(), s.end()));
    return 0;
}

输出
aab
aba
baa

面试题实战:数位重排

#include <bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin >> n;
    while(n--){
        string str;
        cin >> str;
        int oldVal = stoi(str);
        bool flag = false;
        do{
            int newVal = stoi(str);
            if(oldVal != newVal && (oldVal % newVal == 0 || newVal % oldVal == 0)){
                flag = true;
            }
        }while(next_permutation(str.begin(), str.end()));
        cout << (flag ? "Possible" : "Impossible") << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值