全排序问题

FJNU.1302

Description
将一个字符组全排序

Input
一个长度小于10的字符串,该字符串由数字1~9组成。字符不会重复出现。

Output
按数字在输入串中出现的次序从小到大的顺序输出该字符组的全排序

Sample Input
132

Sample Output
1 3 2
1 2 3
3 1 2
3 2 1
2 1 3
2 3 1

My Program

#include<iostream>
#include
<string.h>
#include
<algorithm>
#define N 10
using namespace std;
int num[N];

bool compare(char a,char b)
{
    
return num[a-'0']>num[b-'0'];
}


int main()
{
    
char str[10];
    
int n,i;
    cin
>>str;
    n
=strlen(str);
    
for(i=0;i<n;i++)
        num[(str[i]
-'0')]=N-i;
    sort(str,str
+n,compare);
    
do
    
{
        
for(i=0;i<n-1;i++)
            cout
<<str[i]<<" ";
        cout
<<str[n-1]<<endl;
    }
while(next_permutation(str,str+n,compare));
    
return 0;
}

YOYO's Note: 
STL,写好比较函数就好了……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值