数组顺时针旋转90度后输出(行列相等用单向链表实现)
来源:互联网 发布:ife矩阵分析案例 编辑:程序博客网 时间:2024/05/22 21:27
//用单向链表实现二维数组的操作运算
#include<iostream>
#include<iomanip>
using namespace std;
struct ArrayNode
{
int row;
int column;
int data;
ArrayNode *next;
};
ArrayNode *createArray(int n);//用单向链表创建二维数组
void getArrayData(int n,ArrayNode *head);//显示二维数组各元素
int arrayDiagonal(int n,ArrayNode *head);//求二维数组对角线之和
int arraySum(ArrayNode *head);//求二维数组总和
void outputSpinArray(int n,ArrayNode *head);//数组顺时针旋转90度后输出
int main()
{
ArrayNode *ListHead=NULL;
int n;
cout<<"请输入二维数组的行数(已知行数与列数相等):"<<endl;//二维数组行列相等,总元素个数就等于n*n
cin>>n;
if(n>1)
{
ListHead=createArray(n);
cout<<"您输入的二维数组为:"<<endl;
getArrayData(n,ListHead);
cout<<"此二维数组的对角线之和为:"<<arrayDiagonal(n,ListHead)<<endl;
cout<<"此二维数组的非对角线之和为:";
cout<<arraySum(ListHead)-arrayDiagonal(n,ListHead)<<endl;
cout<<"此数组顺时针旋转90度后为:"<<endl;
outputSpinArray(n,ListHead);
}
else
cout<<"二维数组的行数不能小于2!"<<endl;
return 0;
}
//用单向链表创建二维数组
ArrayNode *createArray(int n)
{
ArrayNode *head=NULL,*tail=NULL,*temp=NULL;
int num;
cin>>num;
head=new ArrayNode;
if(head==NULL)
{
cerr<<"no memory available!"<<endl;
return NULL;
}
else
{
head->data=num;
head->row=1;//数组行列均从1开始计数
head->column=1;
head->next=NULL;
tail=head;
}
for(int i=2,j=1,k=1;k<=n*n-1;i++,k++)//i为列数,j为行数,k用来计算输入的总个数
{
cin>>num;
temp=new ArrayNode;
if(temp==NULL)
{
cerr<<"no memory available!"<<endl;
return head;
}
else
{
temp->data=num;
temp->row=j;
//cout<<"j: "<<j<<endl;//调试用
temp->column=i;
//cout<<"i: "<<i<<endl;//调试用
temp->next=NULL;
tail->next=temp;
tail=temp;
if((k+1)%n==0)
j++;
if(i%n==0)
i=0;
//cout<<endl;
}
}
return head;
}
//显示二维数组各元素
void getArrayData(int n,ArrayNode *head)
{
ArrayNode *put=head;
int i=0;
while(put)
{
if(put->row<=n&&put->column<=n)
{
cout<<setw(4)<<put->data;
i++;
}
if(i%n==0)
cout<<endl;
put=put->next;
}
cout<<endl;
}
//求二维数组对角线之和
int arrayDiagonal(int n,ArrayNode *head)
{
ArrayNode *p=head;
int sum=0;
while(p)
{
if((p->row==p->column)||(p->row+p->column==n+1))//行列都从1开始,所以行数+列数=n+1是第二条对角线上的元素
sum+=p->data;
p=p->next;
}
return sum;
}
//求二维数组总和
int arraySum(ArrayNode *head)
{
ArrayNode *p=head;
int sum=0;
while(p)
{
sum+=p->data;
p=p->next;
}
return sum;
}
//数组顺时针旋转90度后输出
//思路:
//旋转前:
// 1 2 3 4
// 5 6 7 8
// 9 10 11 12
// 13 14 15 16
//旋转后:
// 13 9 5 1
// 14 10 6 2
// 15 11 7 3
// 16 12 8 4
//行列均从1开始计数
//可以看到先输出4行1列的数,再输出3行1列..2行1列..1行1列
//之后行数再从4开始,列数则加1..
//推广到n后
//n行1列..n-1行1列..n-2行1列..1行1列
//n行2列..n-1行2列..n-2行2列..1行2列
//n行n列..n-1行n列..n-2行n列..1行n列
//编程先找到n行1列进行输出,之后每找到符合条件的输出一个
//因为是链表要考虑到循环输出,当输出所有n*n个数后跳出循环
void outputSpinArray(int n,ArrayNode *head)
{
ArrayNode *p=head;
int i=1,count=0,space=0,m=n;
while(1)
{
while(p)
{
if(p->row==m&&p->column==i)
{
cout<<setw(4)<<p->data;
count++;
space++;
m--;
}
if(space==n)
{
i++;
m=n;
space=0;
cout<<endl;
}
p=p->next;
}
if(count==n*n)
break;
p=head;
}
cout<<endl;
#include<iostream>
#include<iomanip>
using namespace std;
struct ArrayNode
{
int row;
int column;
int data;
ArrayNode *next;
};
ArrayNode *createArray(int n);//用单向链表创建二维数组
void getArrayData(int n,ArrayNode *head);//显示二维数组各元素
int arrayDiagonal(int n,ArrayNode *head);//求二维数组对角线之和
int arraySum(ArrayNode *head);//求二维数组总和
void outputSpinArray(int n,ArrayNode *head);//数组顺时针旋转90度后输出
int main()
{
ArrayNode *ListHead=NULL;
int n;
cout<<"请输入二维数组的行数(已知行数与列数相等):"<<endl;//二维数组行列相等,总元素个数就等于n*n
cin>>n;
if(n>1)
{
ListHead=createArray(n);
cout<<"您输入的二维数组为:"<<endl;
getArrayData(n,ListHead);
cout<<"此二维数组的对角线之和为:"<<arrayDiagonal(n,ListHead)<<endl;
cout<<"此二维数组的非对角线之和为:";
cout<<arraySum(ListHead)-arrayDiagonal(n,ListHead)<<endl;
cout<<"此数组顺时针旋转90度后为:"<<endl;
outputSpinArray(n,ListHead);
}
else
cout<<"二维数组的行数不能小于2!"<<endl;
return 0;
}
//用单向链表创建二维数组
ArrayNode *createArray(int n)
{
ArrayNode *head=NULL,*tail=NULL,*temp=NULL;
int num;
cin>>num;
head=new ArrayNode;
if(head==NULL)
{
cerr<<"no memory available!"<<endl;
return NULL;
}
else
{
head->data=num;
head->row=1;//数组行列均从1开始计数
head->column=1;
head->next=NULL;
tail=head;
}
for(int i=2,j=1,k=1;k<=n*n-1;i++,k++)//i为列数,j为行数,k用来计算输入的总个数
{
cin>>num;
temp=new ArrayNode;
if(temp==NULL)
{
cerr<<"no memory available!"<<endl;
return head;
}
else
{
temp->data=num;
temp->row=j;
//cout<<"j: "<<j<<endl;//调试用
temp->column=i;
//cout<<"i: "<<i<<endl;//调试用
temp->next=NULL;
tail->next=temp;
tail=temp;
if((k+1)%n==0)
j++;
if(i%n==0)
i=0;
//cout<<endl;
}
}
return head;
}
//显示二维数组各元素
void getArrayData(int n,ArrayNode *head)
{
ArrayNode *put=head;
int i=0;
while(put)
{
if(put->row<=n&&put->column<=n)
{
cout<<setw(4)<<put->data;
i++;
}
if(i%n==0)
cout<<endl;
put=put->next;
}
cout<<endl;
}
//求二维数组对角线之和
int arrayDiagonal(int n,ArrayNode *head)
{
ArrayNode *p=head;
int sum=0;
while(p)
{
if((p->row==p->column)||(p->row+p->column==n+1))//行列都从1开始,所以行数+列数=n+1是第二条对角线上的元素
sum+=p->data;
p=p->next;
}
return sum;
}
//求二维数组总和
int arraySum(ArrayNode *head)
{
ArrayNode *p=head;
int sum=0;
while(p)
{
sum+=p->data;
p=p->next;
}
return sum;
}
//数组顺时针旋转90度后输出
//思路:
//旋转前:
// 1 2 3 4
// 5 6 7 8
// 9 10 11 12
// 13 14 15 16
//旋转后:
// 13 9 5 1
// 14 10 6 2
// 15 11 7 3
// 16 12 8 4
//行列均从1开始计数
//可以看到先输出4行1列的数,再输出3行1列..2行1列..1行1列
//之后行数再从4开始,列数则加1..
//推广到n后
//n行1列..n-1行1列..n-2行1列..1行1列
//n行2列..n-1行2列..n-2行2列..1行2列
//n行n列..n-1行n列..n-2行n列..1行n列
//编程先找到n行1列进行输出,之后每找到符合条件的输出一个
//因为是链表要考虑到循环输出,当输出所有n*n个数后跳出循环
void outputSpinArray(int n,ArrayNode *head)
{
ArrayNode *p=head;
int i=1,count=0,space=0,m=n;
while(1)
{
while(p)
{
if(p->row==m&&p->column==i)
{
cout<<setw(4)<<p->data;
count++;
space++;
m--;
}
if(space==n)
{
i++;
m=n;
space=0;
cout<<endl;
}
p=p->next;
}
if(count==n*n)
break;
p=head;
}
cout<<endl;
}
以上代码均在VS2005下编写
- 数组顺时针旋转90度后输出(行列相等用单向链表实现)
- 算法:数组顺时针、逆时针90度旋转
- Rotate Image 数组顺时针旋转90度
- Java实现矩阵顺时针旋转90度
- 顺时针旋转输出数字
- Java 数组转置 方阵顺时针旋转90度
- Java 实现顺时针螺旋二维数组输出
- 矩阵顺时针旋转90度
- 矩阵顺时针旋转90度
- 矩阵顺时针旋转90度
- 矩阵顺时针旋转90度
- 矩阵顺时针旋转90度
- 行列转换(将表旋转90度)
- 行列转换(将表旋转90度)
- 单向链表旋转
- 顺时针输出数组
- 二维数组顺时针输出
- 二维数组旋转90度输出
- 下面是选择顺序容器类型的一些准则
- java字符串与二进制的相互转化
- 寻找逆序对
- 面向对象程序设计
- Javascript正则表达式
- 数组顺时针旋转90度后输出(行列相等用单向链表实现)
- 深入C(关键字)一二三
- JavaMail发送邮件
- Linux不需要磁盘碎片整理
- View类详解
- JAVA对象序列化保存为XML文件的工具类
- linux内存分配函数
- 2011.10.23 poj1423Big Number 伪解题报告
- 网上找的c语言读取配置文件