当前位置:网站首页>7-6 矩阵的局部极小值(PTA程序设计)
7-6 矩阵的局部极小值(PTA程序设计)
2022-07-06 09:22:00 【编程林黛玉】
给定M行N列的整数矩阵A,其中3≤M,N≤10,如果A的非边界元素A[i][j]小于相邻的上下左右4个元素,那么就称元素A[i][j]是矩阵的局部极小值。要求编写程序输出给定矩阵的全部局部极小值及其所在的位置。每行按照“元素值 行号 列号”的格式输出一个局部极小值,其中行、列编号从1开始。要求按照行号递增输出;若同行有超过1个局部极小值,则该行按列号递增输出。若没有局部极小值,则输出“None”。
输入格式:
先在第一行输入矩阵的行数M和列数N,再从第二行开始输入整数矩阵A的所有元素。
输出格式:
按题目要求输出给定矩阵的全部局部极小值及其所在的位置。
输入样例:
在这里给出一组输入。例如:
4 5
9 9 9 9 9
9 3 9 5 9
9 5 3 5 9
9 9 9 9 9
输出样例:
在这里给出相应的输出。例如:
3 2 2
3 3 3
代码(Python):
m,n=map(int,input().split()) #输入行数m和列数n
list1=[] #list1用来存放矩阵
count=0 #用来记录有几个符合条件的值
for i in range(m): #注意二维数组的输入方式
s = input() #一行一行的输入
list1.append([int(n) for n in s.split()]) #对每一行的数用空格分开,split()函数的返回值是一个列表,即将每一行作为一个元素,进行强制类型转换后,加入到list1中
for i in range(1,m-1): #遍历矩阵的每一个内部元素
for j in range(1,n-1):
if list1[i][j]<list1[i-1][j] and list1[i][j]<list1[i][j-1] and list1[i][j]<list1[i+1][j] and list1[i][j]<list1[i][j+1]: #判断其是否小于其上下左右的4个元素
print(list1[i][j],i+1,j+1) #如果小于上下左右4个元素的话,就是矩阵的局部极小值,疏忽局部极小值和它的位置,因为列表从0开始,矩阵的行和列从1开始,所以要加1
count=1 #为了方便判断有没有符合条件的值
if count==0: #count=0表示没有符合条件的值
print("None") #没有的话输出None上面的程序给出了比较详细的注释,以便新手小白参考。程序的思路设计或者代码实现并不是最优的,欢迎各位大佬指正错误或者给出更优质的思路。
我是一只想成为鲲鹏的菜鸟,大家的鼓励是我前进的动力,欢迎大家点赞收藏评论哦!
边栏推荐
- 2. C language matrix multiplication
- 【手撕代码】单例模式及生产者/消费者模式
- MySQL limit x, -1 doesn't work, -1 does not work, and an error is reported
- (original) make an electronic clock with LCD1602 display to display the current time on the LCD. The display format is "hour: minute: Second: second". There are four function keys K1 ~ K4, and the fun
- 20220211-CTF-MISC-006-pure_ Color (use of stegsolve tool) -007 Aesop_ Secret (AES decryption)
- Floating point comparison, CMP, tabulation ideas
- String abc = new String(“abc“),到底创建了几个对象
- [modern Chinese history] Chapter V test
- Write a program to simulate the traffic lights in real life.
- Change vs theme and set background picture
猜你喜欢

MySQL锁总结(全面简洁 + 图文详解)

(超详细二)onenet数据可视化详解,如何用截取数据流绘图

强化学习系列(一):基本原理和概念

Reinforcement learning series (I): basic principles and concepts

Have you encountered ABA problems? Let's talk about the following in detail, how to avoid ABA problems

9.指针(上)

FAQs and answers to the imitation Niuke technology blog project (I)

Service ability of Hongmeng harmonyos learning notes to realize cross end communication

仿牛客技术博客项目常见问题及解答(一)
![[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP](/img/d6/109042b77de2f3cfbf866b24e89a45.png)
[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP
随机推荐
稻 城 亚 丁
C language to achieve mine sweeping game (full version)
Mode 1 two-way serial communication is adopted between machine a and machine B, and the specific requirements are as follows: (1) the K1 key of machine a can control the ledi of machine B to turn on a
canvas基础1 - 画直线(通俗易懂)
vector
[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP
4.分支语句和循环语句
6.函数的递归
MPLS experiment
(original) make an electronic clock with LCD1602 display to display the current time on the LCD. The display format is "hour: minute: Second: second". There are four function keys K1 ~ K4, and the fun
【九阳神功】2017复旦大学应用统计真题+解析
简单理解ES6的Promise
关于双亲委派机制和类加载的过程
The latest tank battle 2022 - full development notes-3
Floating point comparison, CMP, tabulation ideas
受检异常和非受检异常的区别和理解
[中国近代史] 第九章测验
甲、乙机之间采用方式 1 双向串行通信,具体要求如下: (1)甲机的 k1 按键可通过串行口控制乙机的 LEDI 点亮、LED2 灭,甲机的 k2 按键控制 乙机的 LED1
[modern Chinese history] Chapter 9 test
3. C language uses algebraic cofactor to calculate determinant