您的位置:首页 > 其它

矩阵的翻转

2014-11-24 11:13 218 查看
// 翻转矩阵.cpp : 定义控制台应用程序的入口点。
//
#include"stdafx.h"
#include<iostream>
#include<time.h>
#include<algorithm>
usingnamespace
std;
#define M8
int randgener()
{
return(rand()%100);
}
void print(int**
a)
{
for(int
i=0; i<
M; i++)
{
for(int
j=0; j<
M; j++)[/code]
{[/code]
cout<<
a[i][j]<<"\t";[/code]
}[/code]
cout<<
endl;[/code]
}[/code]
}
void swap(int&
a ,int& b)
{
int
temp= a;[/code]
a=
b;[/code]
b=
temp;[/code]
}
voidReserve(int**
a,int m)
{
if(NULL==
a)[/code]
return;[/code]
int
low=0;[/code]
int
high= m;[/code]
while(low<
high)[/code]
{[/code]
swap(a[low][low],
a[low][high]);[/code]
swap(a[low][low],
a[high][high]);[/code]
swap(a[low][low],
a[high][low]);[/code]
for(int
i= low+1;
i<= high-1;
i++)[/code]
{[/code]
swap(a[low][i],
a[i][high]);[/code]
swap(a[low][i],
a[high][m-i]);[/code]
swap(a[low][i],
a[m-i][low]);[/code]
}[/code]
low++;[/code]
high--;[/code]
cout<<
endl;[/code]
}
}[/code]
int main()
{
int**
a=newint*[M];[/code]
int*
b=newint[M*M+1];[/code]
for(int
i=0; i<
M; i++)[/code]
a[i]=
b+ i*M;[/code]
generate(b,
b+M*M,
randgener);//初始化矩阵---好方法[/code]
print(a);[/code]
Reserve(a,
M-1);[/code]
cout<<
endl;[/code]
print(a);[/code]
return0;[/code]
}
一圈一圈的转
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: