求一个5X5矩阵的对角线元素之和C++
1个回答
展开全部
#include<iostream>
#include<iomanip>
using namespace std;
//以单向链表结构表示二维数组
struct LinkNode
{
int row; //行号
int column; //列号
int date; //数据
LinkNode *next; //下个结点
};
LinkNode *Create( int n ) //建立单向链表
{
LinkNode *head=NULL, *s=NULL, *p=NULL;
int num;
cin >> num;
head = new LinkNode;
if( head == NULL ){
cerr << "no number\n";
return NULL;
}
else{
head->date = num;
head->row = 1;
head->column = 1;
head->next = NULL;
p = head;
}
for( int i=2,j=1,k=1;k<n*n;i++,k++ ){ //i为列数,j为行数,k为总个数
cin >> num;
s = new LinkNode;
if( s == NULL ){
cerr << "未输入\n";
return head;
}
else{
s->date = num;
s->row = j;
s->column = i;
s->next = NULL;
p->next = s;
p = p->next;
if( (k+1)%n==0 ) j++;
if( i==n ) i = 0;
}
}
return head;
}
void Show( LinkNode *head,int n ) //输出数组
{
int i = 0;
while( head ){
cout << head->date << " ";
i++;
if( i%n==0 )
cout<<endl;
head = head->next;
}
}
void Diagonal( LinkNode *head, int n ) //求主、副对角线之和
{
int sum = 0;
while( head ){
if( head->row==head->column || (head->row+head->column)==n+1 )
sum += head->date;
head = head->next;
}
cout << "对角线之和:" << sum << endl;
}
void Output( LinkNode *head, int n ) //顺时针旋转90°输出
{
LinkNode *p=head;
int i=1, j=n;
while( 1 ){
while( p ){
if( p->row==j && p->column==i ){
cout << p->date << " ";
j--;
}
if( (j+1)==1 ){
cout << endl;
i++;
j = n;
}
if( i+1==n && j+1==1 )
break;
p = p->next;
}
p = head;
}
cout << endl;
}
int main()
{
LinkNode *arry=NULL;
int n;
cout << "请输入二维数组的行数(行数=列数>1):\t";
cin >> n;
if( n>1 ){
arry = Create( n );
cout << "您输入的数组为:\n";
Show( arry, n );
Diagonal( arry, n ); //对角线之和
cout << "顺时针旋转90°输出为:\n";
Output( arry, n );
}
}
#include<iomanip>
using namespace std;
//以单向链表结构表示二维数组
struct LinkNode
{
int row; //行号
int column; //列号
int date; //数据
LinkNode *next; //下个结点
};
LinkNode *Create( int n ) //建立单向链表
{
LinkNode *head=NULL, *s=NULL, *p=NULL;
int num;
cin >> num;
head = new LinkNode;
if( head == NULL ){
cerr << "no number\n";
return NULL;
}
else{
head->date = num;
head->row = 1;
head->column = 1;
head->next = NULL;
p = head;
}
for( int i=2,j=1,k=1;k<n*n;i++,k++ ){ //i为列数,j为行数,k为总个数
cin >> num;
s = new LinkNode;
if( s == NULL ){
cerr << "未输入\n";
return head;
}
else{
s->date = num;
s->row = j;
s->column = i;
s->next = NULL;
p->next = s;
p = p->next;
if( (k+1)%n==0 ) j++;
if( i==n ) i = 0;
}
}
return head;
}
void Show( LinkNode *head,int n ) //输出数组
{
int i = 0;
while( head ){
cout << head->date << " ";
i++;
if( i%n==0 )
cout<<endl;
head = head->next;
}
}
void Diagonal( LinkNode *head, int n ) //求主、副对角线之和
{
int sum = 0;
while( head ){
if( head->row==head->column || (head->row+head->column)==n+1 )
sum += head->date;
head = head->next;
}
cout << "对角线之和:" << sum << endl;
}
void Output( LinkNode *head, int n ) //顺时针旋转90°输出
{
LinkNode *p=head;
int i=1, j=n;
while( 1 ){
while( p ){
if( p->row==j && p->column==i ){
cout << p->date << " ";
j--;
}
if( (j+1)==1 ){
cout << endl;
i++;
j = n;
}
if( i+1==n && j+1==1 )
break;
p = p->next;
}
p = head;
}
cout << endl;
}
int main()
{
LinkNode *arry=NULL;
int n;
cout << "请输入二维数组的行数(行数=列数>1):\t";
cin >> n;
if( n>1 ){
arry = Create( n );
cout << "您输入的数组为:\n";
Show( arry, n );
Diagonal( arry, n ); //对角线之和
cout << "顺时针旋转90°输出为:\n";
Output( arry, n );
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询