渴望学到知识的菜鸟


私信TA

用户名:ldhskd

访问量:30336

签 名:

这小伙子人行,能处!

等  级
排  名 115
经  验 7670
参赛次数 1
文章发表 48
年  龄 18
在职情况 学生
学  校
专  业

  自我简介:

解题思路:

通过双层循环来控制横坐标和纵坐标

其中,对于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分

22 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

用数组写的,动态分配内存,用一维表示二维
#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;
}
2024-03-12 22:12:41
#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;
}
大佬帮忙看看哪里错了
2022-10-31 20:04:36
#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;
}
2022-01-21 15:39:28
有问题,没有考虑到输入负数的情况:(
2022-01-21 15:37:33
#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;
}
2022-01-09 06:48:26
如果第一个数的绝对值最大,就出错了,此时indexx和indexy没有赋值为1,它们的输出为0.
2021-11-13 01:07:55
不是说输出绝对值最大的数组吗
2021-05-23 15:54:51
  • «
  • 1
  • »