📄 mofang.cpp
字号:
//This program is to create a matrix named "Mo Fang"
//The sum of the data which is in one colum,row,diagonal of the matrix is same
//Author : QiuChu
//Date : 04-09-20
#include <iostream.h>
void main()
{
int dimension; //The dimension of the matrix
//Let user to enter the dimension of the mofang matrix
cout<<"please enter the dimension :"<<endl;
cin>>dimension;
//Computing the max number of the matrix
int max = dimension * dimension;
//int matrix[100];
int *matrix = new int[max]; //动态地创建数组,大小为max
int row = 0; //The row of the number in the matrix
int col = 0; //The column of the number in the matrix
//Create the mofang matrix
for(int num = 1 ; num <= max ; num ++)
{
//Computing the row and col of the target number
if(num % dimension == 1)
{
if(num == 1)
{
row = 0;
col = dimension / 2;
}
else
row ++;
}
else
{
row --;
col ++;
}
if(row == -1)
row = dimension - 1;
if(col == dimension)
col = 0;
//Set the number into the matrix with the corresponding row and column
int offset = row * dimension + col;
matrix[offset] = num;
}
//Output the matrix
for(int i = 0 ; i < dimension ; i ++)
{
for(int j = 0 ; j < dimension ; j ++)
{
cout<<matrix[i * dimension + j]<<"\t";
}
//New row
cout<<endl;
}
delete[] matrix; //释放内存
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -