You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改4x4矩阵中出现次数≥2的行内指定值为0?

问题:4x4矩阵指定值批量修改异常

需求说明

  • 统计4x4矩阵每行中指定值的出现次数
  • 将该值出现次数≥2的行内所有该值修改为0

当前代码已完成统计功能,但修改操作仅处理了部分符合条件的值,未实现整行替换。


原代码

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define N 4

int main() {

int mat[N][N] = { {6, 75, 45, 6}, {30, 6, 77, 64}, {15, 35, 6, 43}, {6, 95, 47, 6} };
int val;
int i, j;
int count_occ = 0;
srand(time(NULL));

/* for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
        mat[i][j] = (rand() % 99 + 1);  
    }
} */

printf("Matrix original: \n");
for (i = 0; i < N; i++) {
    for (j = 0; j < N; j++) {
        printf("%3d", mat[i][j]);
    }
    printf("\n");
}

puts("");

printf("Insert a value to search for: ");
scanf("%d", &val);

puts("");

// Counting occurrencies of a value in each row
for (i = 0; i < N; i++) {
    for (j = 0; j < N; j++) {
        if (mat[i][j] == val) {
            count_occ++;
            if (count_occ >= 2) {
                mat[i][j] = 0;
            }
        }
    }
    printf("There are %d occurrencies of value %d in row %d. \n", count_occ, val, i);
    count_occ = 0;
}

puts("");

printf("Matrix modified: \n");
for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
        printf("%3d", mat[i][j]);
    }
    printf("\n");
}

puts("");

}

原运行输出

Matrix original: 
 6 75 45  6
30  6 77 64
15 35  6 43
 6 95 47  6

Insert a value to search for: 6

There are 2 occurrencies of value 6 in row 0. 
There are 1 occurrencies of value 6 in row 1. 
There are 1 occurrencies of value 6 in row 2. 
There are 2 occurrencies of value 6 in row 3. 

Matrix modified: 
  6 75 45  0
 30  6 77 64
 15 35  6 43
  6 95 47  0

问题根源

你当前的逻辑是在统计次数的同时修改元素,仅当count_occ >=2时才修改当前元素,这会导致每行第一次出现的指定值无法被替换——因为第一次统计时count_occ为1,不满足修改条件。

正确的逻辑应该是先统计、后修改:先完整统计每行的指定值出现次数,再根据统计结果决定是否替换整行的所有指定值。


修正后的代码

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define N 4

int main() {

int mat[N][N] = { {6, 75, 45, 6}, {30, 6, 77, 64}, {15, 35, 6, 43}, {6, 95, 47, 6} };
int val;
int i, j;
// 新增数组存储每行的指定值出现次数
int row_counts[N] = {0};
srand(time(NULL));

/* for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
        mat[i][j] = (rand() % 99 + 1);  
    }
} */

printf("Matrix original: \n");
for (i = 0; i < N; i++) {
    for (j = 0; j < N; j++) {
        printf("%3d", mat[i][j]);
    }
    printf("\n");
}

puts("");

printf("Insert a value to search for: ");
scanf("%d", &val);

puts("");

// 第一步:仅统计每行的指定值出现次数,不修改矩阵
for (i = 0; i < N; i++) {
    int count_occ = 0;
    for (j = 0; j < N; j++) {
        if (mat[i][j] == val) {
            count_occ++;
        }
    }
    row_counts[i] = count_occ;
    printf("There are %d occurrencies of value %d in row %d. \n", count_occ, val, i);
}

puts("");

// 第二步:根据统计结果,批量修改符合条件的行
for (i = 0; i < N; i++) {
    if (row_counts[i] >= 2) {
        for (j = 0; j < N; j++) {
            if (mat[i][j] == val) {
                mat[i][j] = 0;
            }
        }
    }
}

printf("Matrix modified: \n");
for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
        printf("%3d", mat[i][j]);
    }
    printf("\n");
}

puts("");

}

修正后运行输出

Matrix original: 
  6 75 45  6
 30  6 77 64
 15 35  6 43
  6 95 47  6

Insert a value to search for: 6

There are 2 occurrencies of value 6 in row 0. 
There are 1 occurrencies of value 6 in row 1. 
There are 1 occurrencies of value 6 in row 2. 
There are 2 occurrencies of value 6 in row 3. 

Matrix modified: 
  0 75 45  0
 30  6 77 64
 15 35  6 43
  0 95 47  0

关键改动

  1. 新增row_counts数组,专门存储每行指定值的出现次数,避免统计与修改操作互相干扰
  2. 拆分逻辑为两个独立阶段:先完整统计所有行的次数,再根据次数批量替换符合条件的行内元素

内容的提问来源于stack exchange,提问作者user13716820

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 17:55:42