V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
yhf
V2EX  ›  问与答

有人能帮我看一下这道题吗? 不胜感激!

  •  
  •   yhf · Feb 14, 2014 · 2888 views
    This topic created in 4458 days ago, the information mentioned may be changed or developed.
    Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.


    #include <iostream>
    using namespace std;

    void setZero(int **mat, int m, int n) {
    bool row[m], col[n];
    memset(row, false, sizeof(row));
    memset(col, false, sizeof(col));
    for (int i = 0; i < m; ++i)
    for (int j = 0; j < n; ++j)
    if (mat[i][j] == 0) {
    row[i] = true;
    col[j] = true;
    }
    for (int i = 0; i < m; ++i)
    for (int j = 0; j < n; ++j)
    if (row[i] || col[j])
    mat[i][j] = 0;
    }

    int main(int argc, char const *argv[]) {
    int **mat;
    mat[3][4] = { 1, 2, 0, 4,
    4, 0, 8, 10,
    3, 8, 0, 12 };
    setZero(mat, 3, 4);
    for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 4; ++j)
    cout << mat[i][j] << ' ';
    cout << endl;
    }
    return 0;
    }

    上面是我写的代码, 编译的时候显示 error: expected expression
    mat[3][4] = { 1, 2, 0, 4,
    ^
    1 error generated.

    各位能帮我看一下吗? 小弟水平很烂, 还请大家轻拍.

    怎么发帖以后缩进全没了.......
    7 replies    1970-01-01 08:00:00 +08:00
    jedyu
        1
    jedyu  
       Feb 14, 2014
    定义过后,不能这么赋值
    yhf
        2
    yhf  
    OP
       Feb 14, 2014
    @jedyu 那应该怎么赋值呢? 麻烦讲详细一点好吗?
    jedyu
        3
    jedyu  
       Feb 14, 2014
    int mat[3][4] = { 1, 2, 0, 4,
    4, 0, 8, 10,
    3, 8, 0, 12 };
    yhf
        4
    yhf  
    OP
       Feb 14, 2014
    @jedyu 可是改成这样赋值以后错误是这样的:
    note: candidate function not viable: no known conversion from
    'int [3][4]' to 'int **' for 1st argument
    void setZero(int **mat, int m, int n) {
    ^
    1 error generated.
    jedyu
        5
    jedyu  
       Feb 14, 2014
    #include <iostream>
    using namespace std;

    void setZero(int mat[3][4], int m, int n)
    {
    if (NULL == mat || 0 ==m || n == 0)
    {
    return;
    }

    bool row[m], col[n];

    for (int i = 0; i < m; i++)
    {
    for (int j = 0; j < n; j++)
    {
    if (mat[i][j] == 0)
    {
    row[i] = true;
    col[j] = true;
    }
    }
    }
    for (int i = 0; i < m; ++i)
    {
    for (int j = 0; j < n; ++j)
    {
    if (row[i] || col[j])
    {
    mat[i][j] = 0;
    }
    }
    }
    }

    int main(int argc, char const *argv[])
    {
    int mat[3][4] = { 1, 2, 0, 4,
    4, 0, 8, 10,
    3, 8, 0, 12 };
    setZero(mat, 3, 4);
    for (int i = 0; i < 3; ++i)
    {
    for (int j = 0; j < 4; ++j)
    {
    cout << mat[i][j] << ' ';
    }

    cout << endl;
    }
    return 0;
    }
    yhf
        6
    yhf  
    OP
       Feb 14, 2014
    @jedyu 你这样编译是没有错误的, 但是原题的意思是不知道矩阵的大小的, 所以setZero()里面传递参数的时候不能直接把mat[3][4]传进去的吧....
    jedyu
        7
    jedyu  
       Feb 14, 2014
    那你就转成char **, 然后 *(mat + i * j) = ....
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2506 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 15:38 · PVG 23:38 · LAX 08:38 · JFK 11:38
    ♥ Do have faith in what you're doing.