-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathQ1-6.CPP
60 lines (55 loc) · 1012 Bytes
/
Q1-6.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*******************************************************
题目描述:
如果以m*n矩阵中某个元素为0,则将它所在的行和列都置为0
Date:2014-03-18
********************************************************/
#include<stdio.h>
#include<string.h>
/*
由于我的编译器不支持C99,这里只能将数组row[m]和col[n]作为参数传入
*/
void zeroMatrix(int (*A)[4],int *row,int *col,int m,int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(A[i][j] == 0)
{
row[i] = true;
col[j] = true;
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(row[i] || col[j])
A[i][j] = 0;
}
int main()
{
int A[3][4] =
{
{1,4,6,9},
{2,0,5,3},
{3,6,3,0},
};
int row[3];
int col[4];
memset(row,0,sizeof(row));
memset(col,0,sizeof(col));
int i,j;
printf("the orginal matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
printf("%d ",A[i][j]);
printf("\n");
}
zeroMatrix(A,row,col,3,4);
printf("now the the matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
printf("%d ",A[i][j]);
printf("\n");
}
return 0;
}