C++写矩阵的转置
(2019年2月19日注:这篇文章原先发在自己github那边的博客,时间是2017年2月5日)
对于任意非n阶矩阵的转置,用c++应该怎么写代码,思考了一下,发现并没有那么简单,上网找到了一个比较好的算法,叫做矩阵原地转置矩阵算法。基于别人的代码,改写成可以使用指针动态分配内存的方法。
先放传送门:C++实现矩阵原地转置算法的实现
原理并不难,那篇文章非常的详细,我不再赘述,下面把改写好的代码发出来。
/*************************************************************************
> File Name: matrix_transpose.cpp
> Author: SongLee
> Modified: JCChan
************************************************************************/
#include<iostream>
using namespace std;
/* 后继 */
int getNext(int i, int m, int n)
{
return (i%n)*m + i / n;
}
/* 前驱 */
int getPre(int i, int m, int n)
{
return (i%m)*n + i / m;
}
/* 处理以下标i为起点的环 */
void movedata(int *mtx, int i, int m, int n)
{
int temp = mtx[i]; // 暂存
int cur = i; // 当前下标
int pre = getPre(cur, m, n);
// 从最后一个数开始,获得它的前驱,直到前驱的值和最后一位值相等,相当于交换的逆过程
while (pre != i)
{
mtx[cur] = mtx[pre];
cur = pre;
pre = getPre(cur, m, n);
}
mtx[cur] = temp;
}
/* 转置,即循环处理所有环 */
void transpose(int *mtx, int m, int n)
{
for (int i = ; i<m*n; ++i)
{
int next = getNext(i, m, n);
while (next > i) // 若存在后继小于i说明重复
next = getNext(next, m, n);
if (next == i) // 处理当前环
movedata(mtx, i, m, n);
}
}
void input(int *mtx, int row, int column) {
for (int i = ; i < row; i++) {
for (int j = ; j < column; j++) {
cout << "请输入矩阵的第" << i + << "行第" << j + << "个元素:";
// 根据矩阵的坐标推算它在一维数组中的位置。
cin >> *(mtx + column*i + j);
}
}
}
/* 输出矩阵 */
void print(int *mtx, int m, int n)
{
for (int i = ; i<m*n; ++i)
{
if ((i + ) % n == )
cout << mtx[i] << "\n";
else
cout << mtx[i] << " ";
}
}
/* 测试 */
int main()
{
int row, column;
cout << "请输入矩阵的行数:";
cin >> row;
cout << "请输入矩阵的列数:";
cin >> column;
int *matrix = new int[row*column];
input(matrix, row, column);
cout << "Before matrix transposition:" << endl;
print(matrix, row, column);
transpose(matrix, row, column);
cout << "After matrix transposition:" << endl;
print(matrix, column, row);
delete[] matrix;
system("pause");
return ;
}
结果如下

对于n阶方阵来说,情况则简单的多,同样放上代码。
#include<iostream>
using namespace std;
void move(int *matrix, int n)
{
int i, j, k;
for (i = ; i<n; i++)
for (j = ; j<i; j++)
{
k = *(matrix + i*n + j);
*(matrix + i*n + j) = *(matrix + j*n + i);
*(matrix + j*n + i) = k;
}
}
int main()
{
int n, i, j;
int *p;
cout << "请输入矩阵的维数:";
cin >> n;
p = new int[n*n];
cout << "输入矩阵的元素" << endl;
for (i = ; i<n; i++)
for (j = ; j<n; j++)
{
cout << "第" << i + << "行第" << j +
<< "个元素为:";
cin >> p[i*n + j];
}
cout << "输入的矩阵的为:" << endl;
for (i = ; i<n; i++)
{
for (j = ; j<n; j++)
cout << p[i*n + j] << " ";
cout << endl;
}
move(p, n);
cout << "转置后的矩阵的为:" << endl;
for (i = ; i<n; i++)
{
for (j = ; j<n; j++)
cout << p[i*n + j] << " ";
cout << endl;
}
delete[] p;
system("pause");
return ;
}
C++写矩阵的转置的更多相关文章
- C语言两种方式实现矩阵的转置
#include"stdio.h" typedef struct{ int i,j; int v; }Triple; typedef struct{ Triple date[]; ...
- C语言 矩阵的转置及矩阵的乘法
C语言 矩阵的转置及矩阵的乘法 //凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 1.矩阵的转置 #include<stdio.h> #defi ...
- 关于python中矩阵的实现和矩阵的转置
python中矩阵的实现是靠序列,,, 序列有很多形式, 其实矩阵是现实生活中的东西,把现实生活中的结构转换到程序中. 就需要有个实现的方法,而这种路径是多种多样的. 下面给出一个把矩阵转换成pyth ...
- C语言-实现矩阵的转置-随机函数产生随机数并赋予数组中-190222
//编写程序,实现矩阵的转置(行列互换). #include <stdio.h> #include <conio.h> #include <stdlib.h> ][ ...
- 为何D3D11的几个矩阵需要转置?
在学习D3D11的时候遇到一个问题,事情是这样的: D3D11引入了常量缓存(const buffer)用来实现数据的高速传输,这块儿buffer是CPU Only Write,GPU Only Re ...
- [CSDN_Markdown] 使用LaTeX写矩阵
简介 LaTeX 的公式功能非常强大,一次性讲全不是件容易的事情.将LaTeX 的这些功能分成较小的相互独立的部分来讲,一方面方便大家单独查阅:另一方面,所有[CSDN_Markdown]相关的文章都 ...
- nyoj299——如何优雅的写矩阵快速幂
Matrix Power Series 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Given a n × n matrix A and a positive i ...
- c++数组-矩阵的转置
#include <iostream> using namespace std; int main(){ ][]={{,,},{,,}}; ][]; ;j<;j++){ ;i< ...
- ytu 1050:写一个函数,使给定的一个二维数组(3×3)转置,即行列互换(水题)
1050: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 154 Solved: 112[ ...
随机推荐
- 快速创建vuepress项目(使用vuepress写文档)
vuepress的官方文档:https://vuepress.vuejs.org/zh/guide/ 参考:https://segmentfault.com/a/1190000016333850 ht ...
- Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve com.android.support:appcompat-v7:26.1.0
android studio 3.0 出现此问题可能是因为 你的android studio 时脱机状态 无法下载资源 这时候你可以点击左上角分File->Other Settings-> ...
- CF982F The Meeting Place Cannot Be Changed
题意:给你一张有向图,某人会任意选择起点然后走无穷多步,问是否存在一个点(要求输出)不管他起点在何处怎么走都必经?n<=100005,m<=500005. 标程: #include< ...
- mysql 监控及优化——转载自http://www.cnblogs.com/suansuan/
1.Mysql连接数 Mysql默认最大连接数为100. 设置Mysql的最大连接数,在Mysql的配置文件中增加: max_connections = 1000 #Mysql的最大连接数,默认如 ...
- 2816: [ZJOI2012]网络
传送们 把一个点拆成c个即可 浪费时间的水题... //Achen #include<algorithm> #include<iostream> #include<cst ...
- JDK语法糖之switch字串与枚举支持
在JDK1.7之前,switch只支持byte,short,char,int,注意1.5之后的自动拆箱,对应的这四种基础类型的封装类也同样支持Byte,Short,Character,Integer, ...
- openstack实战部署
简介:Openstack系统是由几个关键服务组成,他们可以单独安装,这些服务根据你的云需求工作在一起,这些服务包括计算服务.认证服务.网络服务.镜像服务.块存储服务.对象存储服务.计量服务.编排服务和 ...
- 6_4.springboot2.x数据整合springData介绍
介绍 Spring Data 项目的目的是为了简化构建基于Spring 框架应用的数据访问技术,包括非关系数据库.Map-Reduce 框架.云数据服务等等:另外也包含对关系数据库的访问支持. spr ...
- Largest Rectangle in a Histogram /// 单调栈 oj23906
题目大意: 输入n,,1 ≤ n ≤ 100000,接下来n个数为每列的高度h ,0 ≤ hi ≤ 1000000000 求得最大矩阵的面积 Sample Input 7 2 1 4 5 1 3 34 ...
- Python学习之--迭代器、生成器
迭代器 迭代器是访问集合元素的一种方式.从对象第一个元素开始访问,直到所有的元素被访问结束.迭代器只能往前,不能往后退.迭代器与普通Python对象的区别是迭代器有一个__next__()方法,每次调 ...