[C++]蓝桥杯 ADV-73. 数组输出

输入一个3行4列的数组,找出该数组中绝对值最大的元素、输出该元素及其两个下标值。如有多个输出行号最小的,还有多个的话输出列号最小的。
样例输入
1 2 3 5
-2 5 8 9
6 -7 5 3
样例输出
9 2 4

#include <bits/stdc++.h>
using namespace std;
int main() {
	int a[3][4],max=0,x=0,y=0;
	for(int i=0;i<3;i++){
		for(int j=0;j<4;j++){
			cin>>a[i][j];
			if(max<abs(a[i][j])){
				max=abs(a[i][j]);
				x=i;y=j;				
			}else if(max==abs(a[i][j])){
				if(x==i){
					if(j<y){
						x=i;y=j;				
					}
				}
				if(i<x){
					x=i;y=j;
				}
			}
		}
	}
	cout<<max<<" "<<x+1<<" "<<y+1;
 return 0;
}
发布了87 篇原创文章 · 获赞 15 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43356428/article/details/104903446
今日推荐