-4

这是一个家庭作业。我接近完成它,但我无法克服最后一个驼峰。我打印了一个数组的所有可能组合,但我不知道如何从所有组合中挑选出独特的组合。我已经尝试过这种方式和其他一些变体,但我无法让它工作,我不知道为什么。大小是数组的长度,包括终止输入的 -1 值。Rowdata 是一个 maxsize 为 25 的数组。 PrintFx 只是一个打印函数,带有四个循环来打印最终数组。谢谢,代码如下:

void RearrangeArray(int rowdata[],int Size)
{
int firstindex;//This is the loop control variable which controls the first permutation of the array
int secondindex;//This is the index control variable that controls the second variables     in the array
int temp[MAXROW]= {0};
int thirdindex = 0;

for (firstindex = 0; firstindex<=Size-1; firstindex++)
  {
  for (secondindex=firstindex+1; secondindex<=Size-1; secondindex++)
    {
     if(rowdata[firstindex]!=rowdata[secondindex] || thirdindex == 0)
     {
      temp[firstindex]=rowdata[firstindex];
      rowdata[firstindex]=rowdata[secondindex];
      rowdata[secondindex] = temp[firstindex];
      if(rowdata[firstindex] == rowdata[secondindex])
      {
        thirdindex=thirdindex+1;
      }
      PrintFx(rowdata, Size);
     }
    }
  }
}

Enter row data: 43101 57784 43101 57784 43101 -1
Combination #1: 57784 43101 43101 57784 43101
Combination #2: 43101 57784 43101 57784 43101
Combination #3: 57784 57784 43101 43101 43101
Combination #4: 43101 43101 57784 57784 43101
Combination #5: 43101 43101 43101 57784 57784
Combination #6: 43101 57784 57784 43101 43101
Combination #7: 43101 57784 43101 43101 57784
4

3 回答 3

1
#include <stdio.h>
#include <stdlib.h>

typedef struct pair {
    int data;
    int n;
} Kind;

int cmp(const void *a, const void *b){
    return ((Kind*)a)->data - ((Kind*)b)->data ;
}

Kind *uniq(int data[], int *size){
    int i, pos;
    Kind *wk;

    wk = (Kind*)malloc(*size*sizeof(Kind));
    for(i=0;i<*size;++i){
        wk[i].data = data[i];
        wk[i].n = 1;
    }
    qsort(wk, *size, sizeof(Kind), cmp);
    pos=0;
    for(i=1;i<*size;++i){
        if(wk[pos].data != wk[i].data){
            wk[++pos].data = wk[i].data;
        } else {
            wk[pos].n += 1;
        }
    }
    *size = pos + 1;//new size
    wk = realloc(wk, *size*sizeof(Kind));

    return wk;
}

void print(Kind data[], int ksize, int store[], int size, int depth){
    int i;
    if(depth == size){
        printf("[ ");
        for(i=0;i<size;++i){
            printf("%d ", store[i]);
        }
        printf("]\n");
        return;
    }
    for(i=0;i<ksize;++i){
        if(data[i].n != 0){
            store[depth]=data[i].data;
            data[i].n -= 1;//update
            print(data, ksize, store, size, depth+1);
            data[i].n += 1;//restore
        }
    }
}

void printCombo(int data[], int size){
    Kind *uniq_data;
    int uniq_data_size = size;
    int *wk;

    uniq_data=uniq(data, &uniq_data_size);

    wk=(int*)malloc(size*sizeof(int));
    print(uniq_data, uniq_data_size, wk, size, 0);
    free(wk);
    free(uniq_data);
}

int main(void){
    int data[] = {43101, 57784, 43101, 57784, 43101};
    int size = sizeof(data)/sizeof(int);

    printCombo(data, size);
    return 0;
}
/*
[ 43101 43101 43101 57784 57784 ]
[ 43101 43101 57784 43101 57784 ]
[ 43101 43101 57784 57784 43101 ]
[ 43101 57784 43101 43101 57784 ]
[ 43101 57784 43101 57784 43101 ]
[ 43101 57784 57784 43101 43101 ]
[ 57784 43101 43101 43101 57784 ]
[ 57784 43101 43101 57784 43101 ]
[ 57784 43101 57784 43101 43101 ]
[ 57784 57784 43101 43101 43101 ]
*/
于 2013-04-20T19:32:31.977 回答
1

这个程序解释了给定字符串的所有组合

例如:如果给定字符串是 ICON,则可能的组合是

ICON ICNO IOCN IONC INCO INOC CION CINO COIN CONI CNIO CNOI OICN OINC OCIN OCNI ONIC ONCI NICO NIOC NCIO NCOI NOIC NOCI

#include<stdio.h>
#include<string.h>
//char digits[]="0123456789";
char digits[10][5]=
{
    "ICON","CREW","FARM","OILY","CHOP","ARID","FUND","WAIT","GNAT","TEAR"
};

char str[10];
int top=0;

void push(char a) 
{
    str[top++]=a;
}

char pop() 
{
    return(str[--top]);
}

void generate(char dig[15],int n) 
{
    int i;
    char dig2[15];
    if(n==0) 
    {
        push('\0');
        printf("\n %s",str);
        pop();
    } 
    else 
    {
        for(i=0;dig[i]!='\0';i++) 
        {
            if(dig[i]!=' ') 
            {
                strcpy(dig2,dig);
                push(dig[i]);
                dig2[i]=' ';
                generate(dig2,n-1);
                pop();
            }
        }
    }
}

void main() 
{
    int i;

    for(i=0;i<10;i++) 
    {
        generate(digits[i],4);
    }
} 

http://forgetcode.com/C/1418-Program-For-All-Combination-of-the-Given-String

您可以根据自己的要求轻松修改它。

于 2013-04-20T19:57:37.773 回答
0

我和你的组合对应表。

[ 43101 43101 43101 57784 57784 ]#5
[ 43101 43101 57784 43101 57784 ]
[ 43101 43101 57784 57784 43101 ]#4
[ 43101 57784 43101 43101 57784 ]#7
[ 43101 57784 43101 57784 43101 ]#2
[ 43101 57784 57784 43101 43101 ]#6
[ 57784 43101 43101 43101 57784 ]
[ 57784 43101 43101 57784 43101 ]#1
[ 57784 43101 57784 43101 43101 ]
[ 57784 57784 43101 43101 43101 ]#3

Combination #1: 57784 43101 43101 57784 43101
Combination #2: 43101 57784 43101 57784 43101
Combination #3: 57784 57784 43101 43101 43101
Combination #4: 43101 43101 57784 57784 43101
Combination #5: 43101 43101 43101 57784 57784
Combination #6: 43101 57784 57784 43101 43101
Combination #7: 43101 57784 43101 43101 57784
于 2013-04-21T21:50:59.640 回答