反转矩阵代码

#include<reg52.h> //头文件
#define uchar unsigned char //宏定义
#define uint unsigned int

uchar key,n;   //定义变量
uchar code table[]={0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77};//反转法矩阵键盘的各个按键的计算值
uchar code yin[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//共阴极数码管显示0~F

void delay(uint i)//延时函数
{
while(i--);
}

void keyscan()
{

 uchar l,h,i;//定义局部变量,用l得出低4位的值,用h得出高4位的值
 P1=0x0f;//给P1赋值00001111
 l=P1&0x0f;
 if(l!=0x0f)
 {
  delay(100);
  if(l!=0x0f)
  l=P1&0x0f;//若有键按下,得出低四位的值
 }
 P1=0xf0;//给P1赋值11110000
 h=P1&0xf0;
 if(h!=0xf0)
 {
  delay(100);
  if(h!=0xf0)
  h=P1&0xf0;//若有键按下,得出高4位的值
 }
 key=l+h;//高4位的值与低4位的值相加
 for(i=0;i<16;i++)
 {
  if(key==table[i])//通过查表得出n的值
  n=i;
 }
}

void main()
{
 while(1)
 {
  keyscan();
  P0=yin[n];//在数码管上显示相应的键值
 }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
矩阵反转有很多种方法,其中一种比较常见的方法是利用两个指针分别指向矩阵的左上角和右下角,然后交换对应元素。具体步骤如下: 1. 定义两个指针,分别指向矩阵的左上角和右下角。 2. 利用一个循环,每次将左上角指针指向的元素与右下角指针指向的元素进行交换。 3. 然后将左上角指针向右移动一位,右下角指针向左移动一位。 4. 重复上述步骤,直到左上角指针和右下角指针交叉。 下面是一个 Python 代码示例: ```python def reverse_matrix(matrix): row, col = len(matrix), len(matrix[0]) top_left, bottom_right = (0, 0), (row-1, col-1) while top_left[0] < bottom_right[0] and top_left[1] < bottom_right[1]: # 交换上边 for i in range(top_left[1], bottom_right[1]): tmp = matrix[top_left[0]][i] matrix[top_left[0]][i] = matrix[bottom_right[0]-i+top_left[0]][bottom_right[1]] matrix[bottom_right[0]-i+top_left[0]][bottom_right[1]] = tmp # 交换右边 for i in range(top_left[0], bottom_right[0]): tmp = matrix[i][bottom_right[1]] matrix[i][bottom_right[1]] = matrix[bottom_right[0]][bottom_right[1]-i+top_left[1]] matrix[bottom_right[0]][bottom_right[1]-i+top_left[1]] = tmp # 交换下边 for i in range(bottom_right[1], top_left[1], -1): tmp = matrix[bottom_right[0]][i] matrix[bottom_right[0]][i] = matrix[top_left[0]+bottom_right[1]-i][top_left[1]] matrix[top_left[0]+bottom_right[1]-i][top_left[1]] = tmp # 交换左边 for i in range(bottom_right[0], top_left[0], -1): tmp = matrix[i][top_left[1]] matrix[i][top_left[1]] = matrix[bottom_right[0]][top_left[1]+bottom_right[0]-i] matrix[bottom_right[0]][top_left[1]+bottom_right[0]-i] = tmp top_left = (top_left[0]+1, top_left[1]+1) bottom_right = (bottom_right[0]-1, bottom_right[1]-1) return matrix ``` 这个代码可以处理任意大小的矩阵,时间复杂度为 O(n^2),其中 n 是矩阵的行数和列数的最小值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值