#include<iostream>
using namespace std;
class magic //声明魔方阵类magic
{
public:
void getdata(); //输入初值成员函数
void setfirstmagic(); //设置初始魔方成员函数
int generate(); //生成最终魔方成员函数
int printmagic(); //显示魔方成员函数
private:
int m[4][4];
int step;
int first;
int sum;
};
void magic::getdata(){
int a;int b;
cin>>a>>b;
first = a;
step = b;
m[0][0] = b;
}
void magic::setfirstmagic(){
int i,j;
for(i=0;i<4;i++){
for(j=0;j<4;j++){
m[i][j]=first;//进行对数组的赋值;
first=first+step;
}
}
}
int magic::generate(){
int i,j;
for(i=0;i<4;i++){
for(j=0;j<4;j++){
if(i==j||i+j==3){
m[i][j]=32-m[i][j];
}
}
}
}
int magic::printmagic(){
cout << "输出魔方:" << endl;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++)
cout<<m[i][j]<<"\t";
cout<<endl;
}
}
int main(void)
{
magic fun;
fun.getdata();
fun.setfirstmagic();
fun.printmagic();
fun.generate();
fun.printmagic();
return 0;
}
模仿类magic来实现4*4的魔方阵
最新推荐文章于 2023-12-23 09:28:34 发布