2013-04-20 23 views
-4

这是一项家庭作业。我即将完成它,但我无法克服最后一个驼峰。 我打印所有可能的数组组合,但我无法弄清楚如何从所有组合中挑选出独特的组合。 我试过这种方式和其他一些变化,但我不能让它工作,我不明白为什么。 大小是包含用于终止输入的-1值的数组长度。 Rowdata是一个最大数量为25的数组。PrintFx只是一个具有四个循环的打印函数来打印最终数组。谢谢, 下面是代码:打印所有C中数组的独特组合...真的很接近我认为

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 

回答

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 ] 
*/ 
+0

不应该也有结果,如[3 1],[2 3 1]等? – 2013-04-20 19:36:42

+0

'打印所有独特的连击' – BLUEPIXY 2013-04-20 19:39:47

+0

这里是一个输出示例...我不认为由BLUEPIXY提供的答案相当有效.....其实我会把它放在主帖 – Liquidmetal 2013-04-20 19:39:58

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[]=""; 
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

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

+0

谢谢,我很感激。 – Liquidmetal 2013-04-20 20:09:38

+0

这并不排除有重叠重复元素的情况。 – BLUEPIXY 2013-04-20 20:26:59

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