Created
February 7, 2018 01:46
-
-
Save nguyen-thom/c525f892a20e159c2b3d0b8b125b55ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int[][] a = | |
{ {99, 42, 74, 83, 100}, | |
{90, 91, 72, 88, 95}, | |
{88, 61, 74, 89, 96}, | |
{61, 89, 82, 98, 93}, | |
{93, 73, 75, 78, 99}, | |
{50, 65, 92, 87, 94}, | |
{43, 98, 78, 56, 99} }; | |
//gia tri can tim | |
int searchValue = 87; | |
int col = 0; | |
int row = 0; | |
int numberRow = a.length; | |
int numberCol = a[0].length; | |
int totalItemsInArrays = numberCol * numberRow; | |
int itemLoop = 0; | |
//chuyen mang 2 chieu thanh mang mot chieu. | |
for (itemLoop = 0; itemLoop < totalItemsInArrays -1; ++itemLoop ){ | |
//row chay theo row, col chay theo col | |
if(col > numberCol - 1){ | |
col = 0; | |
row = row + 1; | |
} | |
int value = a[row][col]; | |
System.out.println(String.format("Item[%d][%d]=%d",col,row,value)); | |
//check value | |
if(value == searchValue){ | |
//print out | |
System.out.println("Found value search:" + searchValue); | |
System.out.println("at Index:" + row + "," + col); | |
} | |
col++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cháu cảm ơn !