假设有这样一种字符串,它们的长度不大于 10,不重复的输出它的任意排列

分析:由于长度未知,常规的遍历输出方法是很难实现的,递归是个不错的选择。

 

实现:首先完成序列的任意排列输出:

 

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

int g_nCount=0;
string strBuf="";

void OutPut(string,int);

int _tmain(int argc, _TCHAR* argv[])
{
    string str="";
    cout<<"请输入一串字符:"<<endl;
    cin>>str;
    strBuf=str;
    OutPut(str,0);
    system("pause");
    return 0;
}
void OutPut(string str,int i)
{
    int count=0;
    char temp;
    for(count=0;count<str.length();count++)
    {
        if(count>0)
            if(str.at(count)==str.at(count-1))
                continue;
        if(str.at(count)!='~')
        {
            strBuf.at(i)=str.at(count);
            temp=str.at(count);
            str.at(count)='~';
            if(i==str.length()-1)
            {
                cout<<++g_nCount<<":/t/t"<<strBuf<<endl;
            }
            else
                OutPut(str,i+1);
            str.at(count)=temp;
        }
    }
}

那怎么解决字符串中重复字符问题呢?当重复的字符连续出现只替换最后一个出现的就行了,解决方法:先排序,使连续出现的字符相邻出现,一个不足十个字符的字符串排序似乎不需要集合来完成,这里采用了冒泡。

vvoid Sort(string& str)
{
    int i=0,j=0;
    char temp;
    for(i=1;i<str.length();i++)
        for(j=0;j<str.length()-i;j++)
            if(str[j]>str[j+1])
            {
                temp=str[j];
                str[j]=str[j+1];
                str[j+1]=temp;
            }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值