Count the number of equal elements in two arrays (one-dimensional linear method is required)
- IntArray_count (Int* Array1,Int* Array2)
- {
- IntI = 0, j = 0, Count = 0;
- While(I <num1 & J <num2)
- {
- If(Array1 [I] <array2 [J])
- I ++;
- Else If(Array1 [I]> array2 [J])
- J ++;
- Else
- {
- I ++;
- J ++;
- Count ++;
- }
- }
- ReturnCount;
- }
Search and traverse the output two-dimensional array as shown in.
- # Include <stdio. h>
- Int m_print (INT ** data, int N)
- {
- Int I = 0, j = 0;
- // Indicates the exploration sign in the lower left. If the value is 0, the exploration is performed in the upper right corner.
- Int downleft = 1;
- // Count is used for loop exit
- Int num = 0;
- // Number of array elements
- Int loop = N * n-1;
- // Parity mark of n, where 0 is an even number and 1 is an odd number
- Int even = n % 2;
- // Print the first element
- Printf ("% d", * (int *) (data) + I * n + j ));
- While (1)
- {
- // Located at the upper boundary. (J! = N-1 |! Even) is used to process the special situations of the upper-right elements based on the parity of N.
- If (I = 0 & (J! = N-1 |! Even ))
- {
- // Explore to the right
- J ++;
- // Output Element
- Printf ("% d", * (int *) (data) + I * n + j ));
- // Exit the loop after the output is complete.
- If (++ num> = loop) break;
- // Set it to lower left to explore
- Downleft = 1;
- }
- // At the bottom boundary
- If (I = n-1)
- {
- // Explore to the right
- J ++;
- // Output Element
- Printf ("% d", * (int *) (data) + I * n + j ));
- If (++ num> = loop) break;
- // Set it to exploration in the upper right corner
- Downleft = 0;
- }
- // At the left boundary
- If (j = 0)
- {
- // Explore down
- I ++;
- // Output Element
- Printf ("% d", * (int *) (data) + I * n + j ));
- If (++ num> = loop) break;
- // Set it to exploration in the upper right corner
- Downleft = 0;
- }
- // On the right boundary
- If (j = n-1 & (I! = 0 | even ))
- {
- // Explore down
- I ++;
- // Output Element
- Printf ("% d", * (int *) (data) + I * n + j ));
- // Exit the loop after the output is complete.
- If (++ num> = loop) break;
- // Set it to lower left to explore
- Downleft = 1;
- }
- // Explore at the bottom left
- If (downleft)
- {
- I ++;
- J --;
- }
- // Explore in the upper-right corner
- Else
- {
- I --;
- J ++;
- }
- // Output Element
- Printf ("% d", * (int *) (data) + I * n + j ));
- // Exit the loop after the output is complete.
- If (++ num> = loop) break;
- }
- Return 0;
- }
- Int data2 [2] [2] =
- {
- {1, 2 },
- {3, 4}
- };
- Int data3 [3] [3] =
- {
- {1, 2, 6 },
- {3, 5, 7 },
- {4, 8, 9}
- };
- Int data4 [4] [4] =
- {
- {1, 2, 6, 7 },
- {3, 5, 8, 13 },
- {4, 9, 12, 14 },
- {10, 11, 15, 16}
- };
- Int data5 [5] [5] =
- {
- {1, 2, 6, 7, 15 },
- {3, 5, 8, 14,16 },
- {4, 9, 13, 17, 22 },
- {10, 12, 18, 21, 23 },
- {11,19, 20,24, 25}
- };
- Int data6 [6] [6] =
- {
- {1, 2, 6, 7, 15, 16 },
- {3, 5, 8, 14,17, 26 },
- {4, 9, 13, 18, 25, 27 },
- },
- },
- {, 31}
- };
- Int main ()
- {
- M_print (INT **) data2, 2 );
- Printf ("/N ");
- M_print (INT **) data3, 3 );
- Printf ("/N ");
- M_print (INT **) data4, 4 );
- Printf ("/N ");
- M_print (INT **) data5, 5 );
- Printf ("/N ");
- M_print (INT **) data6, 6 );
- Printf ("/N ");
- Return 0;
- }