简体   繁体   中英

Repetitive Permutations with summation bounds

I try to generate all permutations with repetition of a number array by putting bound on summation of values.

Example; I have my array {3,4,5,6} and my bound is 11.

I would like to generate all repetitive permutations reaching and just crossing 11 as:

3 3 3 3 //
3 4 3 3 //
3 3 5 3 //
3 3 3 6 //
3 4 4 3 //
4 4 4 //
6 6 //
6 4 3 //
5 5 5 //
..

So the cardinalty doesnt need to be the same as what we have with array. Thanks for help in advance

I tried the following conversion from Java code, I got it, but still C++ gave the error "Unhandled exception":

void permute(int array[], int start[]){
int sum=0;
for (int i=0; i< sizeof(start)/sizeof(start[0]); i++) {
    sum+= start[i];
}
if (sum >= 11) {
     for (int n=0; n< sizeof(start) / sizeof(start[0]); n++)
       cout << start[n] << " ";
       cout << "\n";
    return;
}
for (int i= 0; i < sizeof(array) / sizeof(array[0]) ; i++) {  
    int* newStart = new int[sizeof(start) / sizeof(start[0]) + 1];
    memcpy (newStart, start, sizeof(start) / sizeof(start[0]) + 1); 
    newStart[sizeof(start) / sizeof(start[0])] = array[i];
             permute(array, newStart);
}

}

 void main ()
 {   
  int array[] = {3,4,5,6};
  int newarray[1];
  for (int i=0; i< sizeof(array)/sizeof(array[0]); i++) {
  newarray[0]=array[i];
  permute(array, newarray); 
}
   system("pause");}

Additionally I would like to keep the indice numbers of all permutations and positions of each member. Example:

Permutation[1119] = [ 3 3 5 3],
 Member[1119][1] = 3,
 Member[1119][2] = 3 etc.

This is not so complicated. Because you're so vague about your language requirements, I took the freedom to invent my own pseudocode:

function generate(int[] array, int bound, int[] solution, int sum)
    if (sum > bound)
        print solution
    else
        for each elt in array
            generate(array, bound, solution ++ [elt], sum + elt)

And call this as

generate([3, 4, 5, 6], 11, [], 0)

This is code for Java:

private static boolean checkConstraint(int[] array) {
    int sum=0;
    for (int i=0; i<array.length; i++) {
        sum+= array[i];
    }
            //we found it, print
    if (sum >= 11) {
        System.out.println(Arrays.toString(array));
        return true;
    }
    return false;
}

public static void permute(int[] array, int[] start){
    if (checkConstraint(start)) {
        return;
    }

    for (int i= 0; i < array.length; i++) {
        int[] newStart= Arrays.copyOf(start, start.length + 1);
        newStart[start.length] = array[i];
        permute(array, newStart);
    }
}

public static void main(String[] args) {
    int[] array= {3,4,5,6};
    for (int i=0; i<array.length; i++) {
        permute(array, new int[] {array[i]});
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM