#include<stdlib.h>
int main()
{
int num
int i
int j
int sign=0
char *str
char ch
char result[1000]
printf("please input the number of strings:\n")
scanf("%d",&num)
//getchar()
for(i=0i<numi++)
{
//if(str!=NULL)
//{
//free(str)
//}
str=(char *)malloc(100*sizeof(char))
printf("please input %d string:\n",i+1)
if(i==0)
{
getchar()
}
ch=getchar()
j=0
while(ch!='\n')
{
str[j]=ch
j++
ch=getchar()
}
str[j]='\0'
printf("the %d string is %s\n",i+1,str)
for(j=0str[j]!='\0'j++)
{
result[sign]=str[j]
sign++
}
if(i<num-1)
{
result[sign]='_'
sign++
}
free(str)
}
result[sign]='\0'
printf("the result is %s\n",result)
return 0
}
运行结果如下,可以自己跑一下程序,就看到结果了。
#include <string>#include <vector>
#include <iostream>
using namespace std
vector<string> do_permutation(string str){
vector<string> res
if (str.size() > 1){
for (size_t i = 0 i != str.size() ++i){
string sub_str = str.substr(0, i) + str.substr(i + 1)
vector<string> tmp = do_permutation(sub_str)
for (size_t j = 0 j != tmp.size() ++j){
res.push_back(str[i] + tmp[j])
}
}
}
else{
res.push_back(str)
}
return res
}
int main(){
size_t n
cout << "n = "
if (cin >> n){
string str
for (size_t i = 0 i != n ++i){
str += static_cast<char>('a' + i)
}
vector<string> res = do_permutation(str)
for (size_t i = 0 i != res.size() ++i){
cout << res[i] << "\n"
}
}
else{
cerr << "输入有误\n"
}
return 0
}
char* str1, *str2gets(str1) //输入字符串str1
for( i = 0i <ni ++ ) //复制前n个字符
{
str2[i] = str1[i] //把str1对应的字符复制到str2中
}
str2[n] = '\0' //字符串结束符,表示字符串str2结束
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)