原题链接:二级C语言-寻找矩阵最值
解题思路:
通过双层循环来控制横坐标和纵坐标
其中,对于max的初始化只需要第一次的时候把输入的值给赋过去就好了
对应的代码就是:
if (i == 1 && j == 1) max = fabs(t);//fabs是取绝对值的函数
之后再对录入进来的值与max比较,如果比max更大的话,就更改max的值,以及对应的横坐标(indexx)和对应的纵坐标(indexy)
对应的代码是:
if (max < t)
{
max = t;
indexx = i;
indexy = j;
}注释:
//n:矩阵的边长 //t:录入进来的临时变量 //indexx:当前位置对应的横坐标 //indexy:当前位置对应的纵坐标 //max:最大值
参考代码:
#include <stdio.h>
#include <math.h>
int main()
{
int n, t, indexx = 0, indexy = 0, max;
scanf("%d", &n);
for(int i = 1 ; i <= n ; i++)
for (int j = 1; j <= n; j++)
{
scanf("%d", &t);
if (i == 1 && j == 1)
max = fabs(t);
if (max < fabs(t))
{
max = fabs(t);
indexx = i;
indexy = j;
}
}
printf("%d %d %d ", max, indexx, indexy);
return 0;
}0.0分
20 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
用数组写的,动态分配内存,用一维表示二维 #include <iostream> using namespace std; struct Loc { int x; int y; }; int main() { int n; cout << "请输入数字 "; cin >> n; int* str; str = new int [n*n]; Loc loc; int max = 0; cout << "开始输入矩阵:"; for (int i = 0; i < n*n; ) { for (int j = 0; j < n; j++) { cin >> str[i + j]; if (str[i + j] > max) { max = str[i + j]; loc.x = i / n+1; loc.y = j + 1; } } i = i + n; } delete[] str; cout << "最大值是 " << max <<endl<< "Location: " << loc.x << loc.y << endl; }#include<stdio.h> int main() { int n; int i,j; int getmax; int x,y; scanf("%d",&n); int b[n][n]; for(i=0;i<=n;++i) { for(j=0;j<=n;++j) scanf("%d",&b[n][n]); } getmax=b[0][0]; for(i=0;i<=n;++i) { for(j=0;j<=n;++j) { if(b[i][j]>getmax) getmax=b[i][j]; x=i; y=j; } } printf("%d %d %d",getmax,x,y); return 0; } 大佬帮忙看看哪里错了#include <stdio.h> #include <math.h> int main() { int n=0; int a=0,a1=0; int b=0,b1=0; int mic=0; int max=0,max1=0; scanf("%d",&n); for(a=1;a<=n;a++) { for(b=1;b<=n;b++) { scanf("%d",&mic); if(abs(mic)>=max){ max=abs(mic); max1=mic; a1=a; b1=b; } } } printf("%d %d %d",max1,a1,b1); return 0; }#include<stdio.h> #include<math.h> int main(){ printf("请输入方阵的长度:\n"); int n,max,t,t1,x,y; scanf("%d",&n); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ scanf("%d",&t); if(i==1&&j==1){ max=fabs(t); } if(max<fabs(t)){ t1=t; max=fabs(t); x=i; y=j; } } } printf("%d %d %d",x,y,t1); return 0; }