c++九宫图小程序
发表于 : 2008-07-07 16:23
#include<iostream>
/*输入n时,请输单数
* 如3*3,5*5.......
* 该程序为单数宫图
*/
using namespace std;
int main()
{
int n;
cout<<"Please see out n is :";
cin>>n;
int sl[n][n];
for(int i=0,temp_a=n/2+1;i<n;i++,temp_a++)
{
if(temp_a>n-1)
temp_a=0;
for(int j=0;j<n;j++)
{
int sl_a,sl_b;
if(temp_a+j+1>n)
sl_a=temp_a+j+1-n;
else
sl_a=temp_a+j+1;
if(temp_a+n-j>n)
sl_b=temp_a+n-j-n;
else
sl_b=temp_a+n-j;
sl[j]=(sl_a-1)*n+sl_b;
if(j==n-1)
cout<<sl[j]<<endl;
else
cout<<sl[j]<<" ";
}
}
return 0;
}
/*输入n时,请输单数
* 如3*3,5*5.......
* 该程序为单数宫图
*/
using namespace std;
int main()
{
int n;
cout<<"Please see out n is :";
cin>>n;
int sl[n][n];
for(int i=0,temp_a=n/2+1;i<n;i++,temp_a++)
{
if(temp_a>n-1)
temp_a=0;
for(int j=0;j<n;j++)
{
int sl_a,sl_b;
if(temp_a+j+1>n)
sl_a=temp_a+j+1-n;
else
sl_a=temp_a+j+1;
if(temp_a+n-j>n)
sl_b=temp_a+n-j-n;
else
sl_b=temp_a+n-j;
sl[j]=(sl_a-1)*n+sl_b;
if(j==n-1)
cout<<sl[j]<<endl;
else
cout<<sl[j]<<" ";
}
}
return 0;
}