递归全排列, i am a student--> student a am i

一个n个数的排列可以由n-1个数的排列生成.只要先生成第n-1个数再插入第n个数就可以了.这是递归算法的中心思想.
#include <iostream.h>

void permu(char a[], int m, int n) {   // 排列算法
    int i;
    char t;
    if (m<n-1) {
       permu(a, m+1, n); //递归
       for (i=m+1;i<n;i++) {
            t=a[m]; a[m]=a[i]; a[i]=t;
            permu(a, m+1, n); //递归
            t=a[m]; a[m]=a[i]; a[i]=t; }
        } else {
         cout<<a<<endl;//屏蔽掉输出
        }
     }
int main() {
    char a[]="ABC";
    permu(a, 0, 3);
    return 0;

i am a student--> student a am i

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


int main()
{
    string str="i am a student",temp,result;

    stringstream ss(str);
    while(ss>>temp)
 {
  result=temp+" "+result;
  cout << result <<endl;
 }
    cout<<result<<endl;
    system("PAUSE");
    return 0;
}  

或者

#include <stdio.h>
void RevStr(char src[])
{
    char *start=src, *end=src, *p=src,e;
    int i=0;
    do
    {
        if(*p==' '|| *p==','|| *p=='.'|| *p==';'|| *p=='!'||! *p)
        {
            i=1;
            end=p-1;
            while(start < end)e= *start, *start++= *end, *end--=e;
        }
        if(i)start=p+1,i=0;
    }while(*p++);
    while(*end++);end-=2;
    while(src < end)e= *src, *src++= *end, *end--=e;
}
int main()
{
    char src[]="i am a !student!!",pause;
    printf("%s/n",src);
    RevStr(src);
    printf("%s/n",src);
    scanf("%c",&pause);
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值