-1

需要制作一个函数来计算数组中具有唯一值的列数

例如:数组

1  2  3  4
2  2  1  4

应该计算两个唯一的列,它们是:

1  3
2  1

结果应该是:2

这是我走了多远,但代码不起作用(给出错误的列数)而且我有点没有想法。

int search_ind(int array[row][col], int r, int c,int column)
{  
    column=0; 
    int i,j,k;

    for(j=0;j<c;j++)
    {
        for(i=0;i<r;i++)
        {
        for(k=i+1;k<r;k++)              
        {
             if (array[i][j] == array[i+k][j])
                 {                                             
                fail=1;
                break;
             }
             else
                 {
            fail=0;
             }

        }
            if (fail == 1)
            {
               break;
            }
        }
        if (fail == 0)
        {
            column++;
        } 

    }
    printf("With indexes:\nColumn count with unique elements:  %d\n\n",column);        
} 
4

1 回答 1

0

您应该执行以下操作:

Starting at column 0 loop over all columns
    Save the value of the cell [this column][row: 0] to a variable
    Starting at row 1 of this column loop through all cells of this column
        if this cell is not equal to the variable: break this loop

    We exited above loop without encountering a break. We can increase our matching-columns counter by 1
We looped through all columns. It's time to return the matching-columns counter.

顺便说一句:为您的索引选择更好的名称。currentColumn、currentLine 等,而不是 i、j、k

于 2013-10-20T18:08:58.653 回答