当前位置:网站首页>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上面的程序给出了比较详细的注释,以便新手小白参考。程序的思路设计或者代码实现并不是最优的,欢迎各位大佬指正错误或者给出更优质的思路。
我是一只想成为鲲鹏的菜鸟,大家的鼓励是我前进的动力,欢迎大家点赞收藏评论哦!
边栏推荐
- [modern Chinese history] Chapter 6 test
- 2.C语言矩阵乘法
- 【九阳神功】2021复旦大学应用统计真题+解析
- MySQL lock summary (comprehensive and concise + graphic explanation)
- 受检异常和非受检异常的区别和理解
- String abc = new String(“abc“),到底创建了几个对象
- 2.初识C语言(2)
- MySQL锁总结(全面简洁 + 图文详解)
- Redis的两种持久化机制RDB和AOF的原理和优缺点
- The difference between overloading and rewriting
猜你喜欢

Difference and understanding between detected and non detected anomalies

4. Binary search

Cookie和Session的区别

hashCode()与equals()之间的关系

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

A piece of music composed by buzzer (Chengdu)

(super detailed II) detailed visualization of onenet data, how to plot with intercepted data flow

1.C语言矩阵加减法

Mortal immortal cultivation pointer-1
随机推荐
MATLAB打开.m文件乱码解决办法
20220211-CTF-MISC-006-pure_ Color (use of stegsolve tool) -007 Aesop_ Secret (AES decryption)
稻 城 亚 丁
Leetcode.3 无重复字符的最长子串——超过100%的解法
The difference between abstract classes and interfaces
[modern Chinese history] Chapter 9 test
List set map queue deque stack
5.MSDN的下载和使用
8.C语言——位操作符与位移操作符
【九阳神功】2016复旦大学应用统计真题+解析
MySQL中count(*)的实现方式
【九阳神功】2018复旦大学应用统计真题+解析
2.初识C语言(2)
About the parental delegation mechanism and the process of class loading
[the Nine Yang Manual] 2021 Fudan University Applied Statistics real problem + analysis
关于双亲委派机制和类加载的过程
C language to achieve mine sweeping game (full version)
Custom RPC project - frequently asked questions and explanations (Registration Center)
ArrayList的自动扩容机制实现原理
编写程序,模拟现实生活中的交通信号灯。