gpt4 book ai didi

c - C 列中连续 1 的最短数量

转载 作者:行者123 更新时间:2023-11-30 16:12:40 25 4
gpt4 key购买 nike

我正在学习 C 语言,但我一直在做这个练习,而且我真的不知道如何解决它:

  • 有了一个 1 和 0 的矩阵,找到任意列中连续 1 的最短数量。

一个例子

1 0 1 1 
1 1 1 1
0 1 1 1
1 1 1 1
1 1 1 1

在这种情况下,最短的数量是左列中的 2 个连续 1。

这是我的代码:

#include <stdio.h>

int main(){

int i, j;
int a[100][100];
int minimum=999999;
int columns,rows;

printf("Number of Rows:");
scanf("%i",&rows);

printf("Number of Columns:");
scanf("%i",&columns);

for (i=0;i<rows;i++) {
for (j=0;j<columns;j++) {
scanf("%i",&a[i][j]);
}
}

for (j=0;j<columns;j++) {
int count_1s=0;

for (i=0;i<rows;i++) {
if (a[i][j]==1) {
count_1s++;
}
else {
count_1s=0;
}
}

if(count_1s>0&&count_1s<minimum) {
minimum=count_1s;
}
printf("%i ",count_1s);
}
printf("%i\n",minimum);
}

最佳答案

有多种方法可以做到这一点,但每种方法都需要知道上面行的列中的最后一个值是否为 10 。您可以保存int last = a[i][j];循环结束时的值(在开始时初始化为 a[0][j] ),或者简单地使用 a[i][j-1] 。在每种情况下,您对行的内部循环i = 1; i < rows 开始运行。而不是i = 0 .

计算最短列数的循环的简单实现类似于:

    for (int j = 0; j < cols; j++) {
int count_1s = a[0][j]; /* initialize count to 1st val */

for (int i = 1; i < rows; i++) { /* loop from rows = 1 */
if (!a[i][j-1]) /* if last zero */
count_1s = a[i][j]; /* set count to current */
if (a[i][j]) /* if current is one */
count_1s++; /* increment each iteration */
}

if (count_1s < mincols) /* update mincols */
mincols = count_1s;

printf ("%d ", count_1s); /* output col count */
}

导致未定义行为的另一个问题是您未能验证 scanf 的返回。您无法正确使用 scanf 中的任何一个函数系列(或任何与此相关的输入函数),而无需检查返回。否则,您将无法响应匹配失败输入失败或用户通过生成手册EOF取消输入。 (例如,Linux 上的 Ctrl+d 或 Windows 上的 Ctrl+z)。所需要做的就是确保输入和转换成功。示例:

    printf ("Number of Rows: ");
if (scanf ("%d", &rows) != 1 || rows > 100) {
fputs ("error: invalid integer - rows.\n", stderr);
return 1;
}

(注意:您还需要如何保护数组边界,以确保用户输入的行或列数不会超过数组可以容纳的行数或列数)

还有你的10阅读后,您可以使用:

    for (int i = 0; i < rows; i++) {        /* loop reading input */
for (int j = 0; j < cols; j++) {
if (scanf ("%d", &a[i][j]) != 1) {
fputs ("error: invalid integer - a[i][j].\n", stderr);
return 1;
}
}
}

总而言之,你可以这样做:

#include <stdio.h>
#include <limits.h>

int main (void) {

int a[100][100],
rows, cols,
mincols = INT_MAX;

printf ("Number of Rows: ");
if (scanf ("%d", &rows) != 1 || rows > 100) {
fputs ("error: invalid integer - rows.\n", stderr);
return 1;
}

printf ("Number of Cols: ");
if (scanf ("%d", &cols) != 1 || cols > 100) {
fputs ("error: invalid integer - cols.\n", stderr);
return 1;
}

for (int i = 0; i < rows; i++) { /* loop reading input */
for (int j = 0; j < cols; j++) {
if (scanf ("%d", &a[i][j]) != 1) {
fputs ("error: invalid integer - a[i][j].\n", stderr);
return 1;
}
}
}

for (int i = 0; i < cols; i++) /* (added formatting) */
printf (i ? "--" : "-");
putchar ('\n');

for (int j = 0; j < cols; j++) {
int count_1s = a[0][j]; /* initialize count to 1st val */

for (int i = 1; i < rows; i++) { /* loop from rows = 1 */
if (!a[i][j-1]) /* if last zero */
count_1s = a[i][j]; /* set count to current */
if (a[i][j]) /* if current is one */
count_1s++; /* increment each iteration */
}

if (count_1s < mincols) /* update mincols */
mincols = count_1s;

printf ("%d ", count_1s); /* output col count */
}
printf ("\n\nminimum column sequence of 1's: %d\n", mincols);
}

示例使用/输出

使用您的输入数据(并在数据和各个列计数之间添加行后),您将得到:

$ ./bin/mincols
Number of Rows: 5
Number of Cols: 4
1 0 1 1
1 1 1 1
0 1 1 1
1 1 1 1
1 1 1 1
-------
2 4 5 5

minimum column sequence of 1's: 2

这不是唯一的方法,但大多数方法都是相似的。检查一下,如果您还有其他问题,请告诉我。

关于c - C 列中连续 1 的最短数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58278159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com