V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
yhf
V2EX  ›  问与答

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

  •  
  •   yhf · 2014-02-14 13:45:13 +08:00 · 2432 次点击
    这是一个创建于 3736 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 条回复    1970-01-01 08:00:00 +08:00
    jedyu
        1
    jedyu  
       2014-02-14 13:47:06 +08:00
    定义过后,不能这么赋值
    yhf
        2
    yhf  
    OP
       2014-02-14 13:49:19 +08:00
    @jedyu 那应该怎么赋值呢? 麻烦讲详细一点好吗?
    jedyu
        3
    jedyu  
       2014-02-14 13:50:13 +08:00
    int mat[3][4] = { 1, 2, 0, 4,
    4, 0, 8, 10,
    3, 8, 0, 12 };
    yhf
        4
    yhf  
    OP
       2014-02-14 13:51:44 +08:00
    @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  
       2014-02-14 14:21:03 +08:00
    #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
       2014-02-14 14:44:12 +08:00
    @jedyu 你这样编译是没有错误的, 但是原题的意思是不知道矩阵的大小的, 所以setZero()里面传递参数的时候不能直接把mat[3][4]传进去的吧....
    jedyu
        7
    jedyu  
       2014-02-14 14:57:09 +08:00
    那你就转成char **, 然后 *(mat + i * j) = ....
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   6201 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 02:42 · PVG 10:42 · LAX 19:42 · JFK 22:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.