/**
*
*/
package Chapter_8;
/**
* @author MohnSnow
* @time 2015年5月22日 下午2:16:45
*
*/
public class chapter8_1_7 {
/**
* @param argsmengdx
* -fnst
* @title 若M*N的矩阵某个元素为0,将其行和列清零
*/
public static void main(String[] args) {
int[][] toZero = { { 1, 1, 1, 1 }, { 0, 1, 0, 1 }, { 0, 0, 1, 1 } };
int[] row = { 0, 0, 0, 0 };
int[] col = { 0, 0, 0, 0 };
int count=0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
count++;
if (toZero[i][j] == 0) {
row[i] = 1;
col[j] = 1;
}
System.out.print(toZero[i][j] + " ");
if (count%4 == 0){
System.out.println();
}
}
}
System.out.println("---------------------我是分割线------------------------------- ");
count = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
count++;
if (row[i] == 1 || col[j] == 1) {
toZero[i][j] = 0;
}
System.out.print(toZero[i][j] + " ");
if (count % 4 == 0) {
System.out.println();
}
}
}
}
}
CTCI---8.1.7
最新推荐文章于 2015-05-25 17:20:17 发布