What is the best algorithm for this array-comparison problem?

后端 未结 4 532
囚心锁ツ
囚心锁ツ 2021-02-01 08:10

What is the most efficient for speed algorithm to solve the following problem?

Given 6 arrays, D1,D2,D3,D4,D5 and D6 each containing 6 numbers like:

D1[0         


        
4条回答
  •  清酒与你
    2021-02-01 08:46

    I was a little bit confused by your question, but I think I have it enough to help you get started.

    #define ROW 6
    #define COL 6
    
    int D[ROW][COL]; // This is all of your D arrays in one 2 dimensional array.
    

    Next you should probably use nested for loops. Each loop will correspond to a dimension of D. Remember that the order of your indexes matters. The easiest way to keep it straight in C is to remember that D[i] is a valid expression even when D has more than one dimension (and would evaluate to a pointer to a row : a sub array).

    If you can't change the independent D arrays to one multidimensional array you could easily make a pointer array whose members point to the heads of each of those arrays and achieve the same effect.

    Then you can use the break statement to break out of the inner loop after you have determined that the current D[i] doesn't match the ans.

提交回复
热议问题