我想要做的是找到重复其内容的一维数组的每个排列.
例如
int array[]={1,2,3};
for(i=0;i<3;i++){
next_permutation(array,array+3)
for(int j=0;j<=3;j++){
printf("%d ",array[j]);
}
printf("\n");
}
Run Code Online (Sandbox Code Playgroud)
将返回:
1 2 3
1 3 2
2 1 3
etc...
Run Code Online (Sandbox Code Playgroud)
我希望函数返回的内容:
1 1 1
1 1 2
1 2 1
2 1 1
1 2 2
2 2 1
2 1 2
1 1 3
1 3 1
3 1 1
etc...
Run Code Online (Sandbox Code Playgroud)
有功能可以做到吗?
提前谢谢,Erik
这是我的代码的一部分:
#include <stdio.h>
#include<string>
#include<string.h>
#include<algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main(){
FILE *in=fopen("C.in","r");
//freopen("C.out","w",stdout);
int maxl=0;
int i;
string word;
vector<string> words;
while(!feof(in)){
fscanf(in,"%s ",word.c_str());
int t=strlen(word.c_str());
if(t>maxl){
maxl=t;
words.clear();
words.insert(words.end(),word);
}else if (t==maxl){
words.insert(words.end(),word);
}
}
Run Code Online (Sandbox Code Playgroud)
问题发生在
words.insert(words.end,word)
Run Code Online (Sandbox Code Playgroud)
而
word
Run Code Online (Sandbox Code Playgroud)
包含我文件中的单词,矢量项
words[i]
Run Code Online (Sandbox Code Playgroud)
包含一个空字符串.
这怎么可能?