OOP 下一个排列(函数模板)

题目描述

输入一个序列,输出其下一个字典序的排列

如:输入1 2 3,

下一个为1 3 2,

下一个为2 1 3,

下一个为2 3 1,

下一个为3 1 2,

下一个为3 2 1(最大的字典序排列)

下一个为1 2 3(重新返回最小的字典序排列)

......

输入有多种类型:

C:表示char型数据

I:表示int型数据

D:表示double型数据

S:表示string型数据

输入

第1行:一个整数n,表示测试次数

以下n行:每行为 C/I/D/S k ...k个数据(用空格分隔)

输出

对每个测试数据输出其下一个字典序排列

------------------------------------------------------

输入样例:

4
C 6 a b d c f e
I 4 1 2 3 4
D 2 4.3 2.1
S 4 ho ha he he

输出样例:

a b d e c f
1 2 4 3
2.1 4.3
ho he ha he

AC代码:

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

template <typename T>
void nextLexicographicalPermutation(T *sequence, int size)
{
    if (next_permutation(sequence, sequence + size))
    {
        for (int i = 0; i < size; ++i)
        {
            if (i == size - 1)
            {
                cout << sequence[i];
            }
            else
                cout << sequence[i] << " ";
        }
        cout << endl;
    }
    else
    {
        sort(sequence, sequence + size);
        for (int i = 0; i < size; ++i)
        {
            if (i == size - 1)
            {
                cout << sequence[i];
            }
            else
                cout << sequence[i] << " ";
        }
        cout << endl;
    }
}

int main()
{
    int n;
    cin >> n;

    for (int i = 0; i < n; ++i)
    {
        char type;
        cin >> type;

        if (type == 'C')
        {
            int k;
            cin >> k;
            char *sequence = new char[k];
            for (int j = 0; j < k; ++j)
            {
                cin >> sequence[j];
            }
            nextLexicographicalPermutation(sequence, k);
            delete[] sequence;
        }
        else if (type == 'I')
        {
            int k;
            cin >> k;
            int *sequence = new int[k];
            for (int j = 0; j < k; ++j)
            {
                cin >> sequence[j];
            }
            nextLexicographicalPermutation(sequence, k);
            delete[] sequence;
        }
        else if (type == 'D')
        {
            int k;
            cin >> k;
            double *sequence = new double[k];
            for (int j = 0; j < k; ++j)
            {
                cin >> sequence[j];
            }
            nextLexicographicalPermutation(sequence, k);
            delete[] sequence;
        }
        else if (type == 'S')
        {
            int k;
            cin >> k;
            string *sequence = new string[k];
            for (int j = 0; j < k; ++j)
            {
                cin >> sequence[j];
            }
            nextLexicographicalPermutation(sequence, k);
            delete[] sequence;
        }
    }

    return 0;
}

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

szuzhan.gy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值