Skip to content

Instantly share code, notes, and snippets.

@JoeUnsung
Created December 8, 2016 16:42
Show Gist options
  • Save JoeUnsung/d71bb16e4f6dc053e744345bada0057c to your computer and use it in GitHub Desktop.
Save JoeUnsung/d71bb16e4f6dc053e744345bada0057c to your computer and use it in GitHub Desktop.
Testcode #2 ; Find out what row and line will make most "1" in the matrix when it is reversed.
package joe;
public class E2_20101062 {
public static void main(String[] args){
int N=8, M=5;
int[][] map = {{1,0,1,1,0},
{0,0,1,0,0},
{1,0,0,0,1},
{0,0,1,0,0},
{0,1,1,1,0},
{1,0,1,0,0},
{1,1,0,0,0},
{0,0,0,1,0}};
int max = 0, cnt = 0, maxRowNum = 0, maxLineNum = 0;
// maxRowNum 찾는 라인
for( int i = 0 ; i < N ; i++){
cnt = 0;
for( int j = 0 ; j < M ; j++){
if (map[i][j] == 0){
cnt++;// 0의 갯수를 세는 카운터
}
}
if (max <= cnt){
max = cnt;
maxRowNum = i; // 가장 많은 0을 가지고 있는 row의 정보를 저장.
}
}
max = 0;
// 가장 많은 0을 가지고 있는 행을 반전시키기.
for (int i = 0 ; i < M ; i++){
if(map[maxRowNum][i] == 0){
map[maxRowNum][i] = 1;
}
else if(map[maxRowNum][i] == 1){
map[maxRowNum][i] = 0;
}
}
// maxLineNum 찾는 라인
for( int i = 0 ; i < M; i++){
cnt = 0;
for( int j = 0 ; j < N ; j++){
if (map[j][i] == 0){
cnt++;
}
}
if (max <= cnt){
max = cnt;
maxLineNum = i; // 가장 많은 0을 가지고 있는 line의 정보를 저장.
}
}
// 가장 많은 0을 가지고 있는 열을 반전시키기.
for (int i = 0 ; i < N ; i++){
if(map[i][maxLineNum] == 0){
map[i][maxLineNum] = 1;
}
else if(map[i][maxLineNum] == 1){
map[i][maxLineNum] = 0;
}
}
// 전체의 0의 갯수를 세서 출력하기
cnt = 0;
for (int i = 0 ; i< N ; i++){
for (int j = 0 ; j < M ; j++){
if(map[i][j] == 1){
cnt++;
}
}
}
System.out.println("answer : " + cnt);
//System.out.println(maxRowNum +" "+ maxLineNum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment