首页 > 试题广场 >

用c++写一个函数,如Foo(const char

[问答题]
用c++写一个函数,如Foo(const char *str),打印出str的全排列,如abc的全排列:abc, acb, bca, dac, cab,cba
#include <iostream>
#include <string>
using namespace std;

void swap(char &ch1,char &ch2){
    char t;
    if(ch!=ch2){
        t=ch1;
        ch1=ch2;
        ch2=t;
    }
}

void func(string &str,int I,int n){
    if(I==n){
        cout << str;
     }else{
        for(string::size_type k=I;k<n;++k){
            swap(str[I],str[k]);
            func(str,i+I,n);
            swap(str[I],str[k]);
         }
     }
}

int main(){
    string str;
    cin >> str;
    func(str,0,str.size());
    return 0;
}
发表于 2017-08-08 10:24:09 回复(0)
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
void Foo(const char*str){
    string s=str;
    cout<<s<<endl;
    while(next_permutation(s.begin(),s.begin()+s.size())){
         cout<<s<<endl;
    }
    return ;
}
int main(){
    const char str[]="abc";
    Foo(str);
    return 0;
}

发表于 2016-07-08 22:54:16 回复(0)
#include<iostream>
using namespace std;
void Foo(char *str,char *begin)
{ 
  
    if(*begin == '\0')  
        cout<<str<<endl; 
    else   
        for(char* p = begin; *p != '\0'; p++)  
        {  
            swap(*begin,*p);
            Foo(str, begin+1);  
            swap(*begin,*p); 
        }  
}
int main()
{
	char *str=new char[100];
	cin>>str;
	Foo(str,str);
	return 0;
}

发表于 2015-10-04 17:06:52 回复(0)
#include<iostream>
using namespace std;
void swap(char *a,char *b)
{
char t=*a;
*a=*b;
*b=t;
}
int ISswap(char *a,int begin,int end)
{
for(int i=0;i<end;i++)
{
if(a[i]==a[end])
{return 0;}
}
return 1;
}

void allrange(char *p,int k,int m)
{
if(k==m)
{
static int n=1;
cout<<p;
}
else
{
for(int i=k;i<=m;i++)
{
if(ISswap(p,k,i))
{
swap(p+k,p+i); allrange(p,k+1,m); swap(p+k,p+i);  }
}
}
}
int main()
{
}

发表于 2015-10-22 11:15:14 回复(0)
  #include <stdio.h> 
  #include <stdlib.h> 
  #include <string.h> 
  

  void swap(char *a, char *b)  
  { 
  if (a != b) { 
  char c = *a; 
  *a = *b; 
  *b = c; 
  } 
  } 
  

  void dfs(char *str, int kth, int len) 
  { 
  if (kth == len) { 
  printf("%s\n", str); 
  return; 
  } 
  

  for (int i = 0; i <= kth; ++i) { 
  swap(&str[i], &str[kth]); 
  dfs(str, kth + 1, len); 
  swap(&str[i], &str[kth]); 
  } 
  } 
  

  int main(void) 
  { 
  char str[] = "abc"; 
  dfs(str, 0, strlen(str)); 
  } 
编辑于 2016-01-13 15:36:52 回复(0)
/**
      * @author revoid
      * @param sb
      * @param ctrs
      * @param btrs
      * @param begin
      * @param end
      */
     public static void fooGet(StringBuilder sb, char []ctrs, boolean[]btrs, int begin, int end)
     {
         if(begin == end)
         {
             System.out.println(sb);
             
             return;
         }
         
         for(int i = 0; i < ctrs.length; i++)
         {
             if(false == btrs[i])
             {
                 sb.append(ctrs[i]);
                 
                 btrs[i] = true;
                 
                 fooGet(sb, ctrs, btrs, begin + 1, end);
                 
                 sb.deleteCharAt(sb.length() - 1);
                 
                 btrs[i] = false;
             
             }
         }
     }
  

编辑于 2016-01-13 15:36:59 回复(0)
#include <iostream>
#include <algorithm>
#include <string>

void Foo(const char * str)
{
 using namespace std;

 if (str==NULL || *str==NULL)
  return;

 string s(str);
 int c=0;
 sort(s.begin(),s.end());

 cout<<s<<endl;
 c++;
 for (;next_permutation(s.begin(),s.end());)
 {
  cout<<s<<endl;
  c++;
 }
 cout<<c<<"种排列"<<endl;
}

int main()
{ 
 using namespace std;

 string s;
 cin>>ws;
 getline(cin,s);
 Foo(s.c_str());
 return 0;
}
发表于 2015-03-06 17:42:18 回复(0)
234234
发表于 2014-11-28 13:12:48 回复(0)
next_permutation
发表于 2014-11-09 18:14:50 回复(0)
经典的问题,递归
发表于 2014-11-04 16:28:54 回复(2)