小编Eri*_*ous的帖子

是否有像next_permutation这样的函数,但是对于重复的排列?

我想要做的是找到重复其内容的一维数组的每个排列.

例如

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

c++ arrays combinations permutation repeat

4
推荐指数
1
解决办法
1746
查看次数

将字符串放入向量中会产生空字符串

这是我的代码的一部分:

#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)

包含一个空字符串.

这怎么可能?

c++ visual-c++

0
推荐指数
1
解决办法
110
查看次数

标签 统计

c++ ×2

arrays ×1

combinations ×1

permutation ×1

repeat ×1

visual-c++ ×1