鱼C论坛

 找回密码
 立即注册
查看: 2440|回复: 1

[已解决]这个运行时错误,要怎么改

[复制链接]
发表于 2022-11-13 18:29:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本题目要求读入M(最大为10)行N(最大为15)列个元素,找出其中最大的元素,并输出其行列值。

输入格式:
输入在第一行中给出行数m和列数n。
接下来输入m*n个整数。

输出格式:
输出最大值的行号,列号,值。

输入样例:
2 3
1 2 3
4 5 6
输出样例:
最大值为1行2列的6

1,2,6


#include <stdio.h>
int main()
{
    int a[6][6];
    int m, n;
    int i, j;
    int max;
    int indexx = 0, indexy = 0;
    scanf("%d %d", &m, &n);
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            scanf("%d", &a[i][j]);
            if (i == 0 && j == 0)
            {
                max = a[i][j];
                indexx = 0;
                indexy = 0;
            }
            else
            {
               if (a[i][j] > max)
               {
                         max = a[i][j];
                        indexx = i;
                        indexy = j;
                }
            }
        }
    }
    printf("%d,%d,%d\n", indexx, indexy,max);
    return 0;
}
最佳答案
2022-11-13 18:38:35
本帖最后由 jackz007 于 2022-11-13 18:45 编辑
  1. #include <stdio.h>
  2. int main()
  3. {
  4.     int i , j , m , n , indexx = 0 , indexy = 0              ;
  5.     scanf("%d%d", & m , & n)                                 ;
  6.     int a[m][n]                                              ;
  7.     for(i = 0 ; i < m ; i ++)
  8.     {
  9.         for (j = 0 ; j < n ; j ++)
  10.         {
  11.             scanf("%d", & a[i][j])                           ;
  12.             if(a[indexx][indexy] < a[i][j])  
  13.             {
  14.                 indexx = i                                   ;
  15.                 indexy = j                                   ;
  16.             }
  17.         }
  18.     }
  19.     printf("%d,%d,%d\n", indexx, indexy , a[indexx][indexy]) ;
  20.     return 0                                                 ;
  21. }
复制代码

        编译运行实况:
  1. D:\[00.Exerciese.2022]\C>g++ -o x x.c

  2. D:\[00.Exerciese.2022]\C>x
  3. 2 3
  4. 1 2 3
  5. 4 5 6
  6. 1,2,6

  7. D:\[00.Exerciese.2022]\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-11-13 18:38:35 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2022-11-13 18:45 编辑
  1. #include <stdio.h>
  2. int main()
  3. {
  4.     int i , j , m , n , indexx = 0 , indexy = 0              ;
  5.     scanf("%d%d", & m , & n)                                 ;
  6.     int a[m][n]                                              ;
  7.     for(i = 0 ; i < m ; i ++)
  8.     {
  9.         for (j = 0 ; j < n ; j ++)
  10.         {
  11.             scanf("%d", & a[i][j])                           ;
  12.             if(a[indexx][indexy] < a[i][j])  
  13.             {
  14.                 indexx = i                                   ;
  15.                 indexy = j                                   ;
  16.             }
  17.         }
  18.     }
  19.     printf("%d,%d,%d\n", indexx, indexy , a[indexx][indexy]) ;
  20.     return 0                                                 ;
  21. }
复制代码

        编译运行实况:
  1. D:\[00.Exerciese.2022]\C>g++ -o x x.c

  2. D:\[00.Exerciese.2022]\C>x
  3. 2 3
  4. 1 2 3
  5. 4 5 6
  6. 1,2,6

  7. D:\[00.Exerciese.2022]\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-23 03:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表