#include<iostream>
using namespace std;
class magic
{
public:
magic(int f,int n);
void setfirstmagic();
void generatemagic();
void printmagic();
private:
int m[4][4];
int step;
int first;
int sum;
};
magic::magic(int f,int s)
{
first=f;
step=s;
}
void magic::setfirstmagic()
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
m[i][j]=first;
first+=step;
}
cout<<"初始魔方为:"<<endl;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
cout<<m[i][j]<<'\t';
if(j==3)
{
cout<<endl;
}
}
}
void magic::generatemagic()
{
sum=m[0][0]+m[3][3];
for(int i=0,j=0;i<4;i++)
{
m[i][j]=sum-m[i][j];
j++;
}
for(int i=0,j=3;i<4;i++)
{
m[i][j]=sum-m[i][j];
j--;
}
}
void magic::printmagic()
{
cout<<"最终魔方为:"<<endl;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
cout<<m[i][j]<<'\t';
if(j==3)
{
cout<<endl;
}
}
}
int main()
{
int f;int s;
cout<<"请输入魔方初值:";
cin>>f;
cout<<endl;
cout<<"请输入各元素差值:";
cin>>s;
cout<<endl;
magic mag1(f,s);
mag1.setfirstmagic();
mag1.generatemagic();
mag1.printmagic();
system("pause");
return 0;
}
4*4魔方程序
最新推荐文章于 2018-12-16 21:28:07 发布