当前位置:网站首页>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
上面的程序给出了比较详细的注释,以便新手小白参考。程序的思路设计或者代码实现并不是最优的,欢迎各位大佬指正错误或者给出更优质的思路。
我是一只想成为鲲鹏的菜鸟,大家的鼓励是我前进的动力,欢迎大家点赞收藏评论哦!
边栏推荐
- Pit avoidance Guide: Thirteen characteristics of garbage NFT project
- 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
- Questions and answers of "basic experiment" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
- (ultra detailed onenet TCP protocol access) arduino+esp8266-01s access to the Internet of things platform, upload real-time data collection /tcp transparent transmission (and how to obtain and write L
- string
- 8.C语言——位操作符与位移操作符
- [the Nine Yang Manual] 2022 Fudan University Applied Statistics real problem + analysis
- Rich Shenzhen people and renting Shenzhen people
- 抽象类和接口的区别
- 甲、乙机之间采用方式 1 双向串行通信,具体要求如下: (1)甲机的 k1 按键可通过串行口控制乙机的 LEDI 点亮、LED2 灭,甲机的 k2 按键控制 乙机的 LED1
猜你喜欢
8.C语言——位操作符与位移操作符
MySQL lock summary (comprehensive and concise + graphic explanation)
PriorityQueue (large root heap / small root heap /topk problem)
1.C语言矩阵加减法
甲、乙机之间采用方式 1 双向串行通信,具体要求如下: (1)甲机的 k1 按键可通过串行口控制乙机的 LEDI 点亮、LED2 灭,甲机的 k2 按键控制 乙机的 LED1
Leetcode. 3. Longest substring without repeated characters - more than 100% solution
Cookie和Session的区别
关于双亲委派机制和类加载的过程
(super detailed II) detailed visualization of onenet data, how to plot with intercepted data flow
魏牌:产品叫好声一片,但为何销量还是受挫
随机推荐
The difference between overloading and rewriting
A piece of music composed by buzzer (Chengdu)
[中国近代史] 第五章测验
2. C language matrix multiplication
[the Nine Yang Manual] 2022 Fudan University Applied Statistics real problem + analysis
[graduation season · advanced technology Er] goodbye, my student days
2.初识C语言(2)
编写程序,模拟现实生活中的交通信号灯。
杂谈0516
Nuxtjs快速上手(Nuxt2)
3.C语言用代数余子式计算行列式
透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
8. C language - bit operator and displacement operator
Custom RPC project - frequently asked questions and explanations (Registration Center)
MySQL事务及实现原理全面总结,再也不用担心面试
7. Relationship between array, pointer and array
This time, thoroughly understand the MySQL index
Aurora system model of learning database
Redis实现分布式锁原理详解
仿牛客技术博客项目常见问题及解答(三)