分页: 1 / 1

c++九宫图小程序

发表于 : 2008-07-07 16:23
greenhill
#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;

}

发表于 : 2008-07-15 22:25
myhrvod
好像有错误吧!

发表于 : 2008-07-16 13:14
lovewine
如此简单吗?不敢相信啊

发表于 : 2008-08-16 15:34
乾水居士
楼主奇才!完全无误!

发表于 : 2008-08-16 16:01
dlwdgl
int sl[n][n];
貌似c++里数组的初始化不能用变量的吧。java可以用int型变量 8)

Re: c++九宫图小程序

发表于 : 2013-08-04 17:24
greenhill
谁来完善双数宫图

Re: c++九宫图小程序

发表于 : 2013-08-04 17:43
月下叹逍遥
greenhill 写了:谁来完善双数宫图
双偶数:
http://www.hsyyf.me/2012/04/%E5%B9%BB%E ... %EF%BC%89/

单偶数:
http://www.hsyyf.me/2012/05/%E5%B9%BB%E ... %EF%BC%89/

python版

Re:

发表于 : 2013-11-26 10:05
johnnr
乾水居士 写了:楼主奇才!完全无误!
试了,没问题。虽然不懂。

Re: c++九宫图小程序

发表于 : 2020-09-16 15:36
greenhill
#include<iostream>
/*输入n时,请输单数
* 如3*3,5*5.......
* 该程序为单数宫图
*/
using namespace std;
int main()
{
int n;
cout<<"Please see out n is :";
cin>>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;
int sl[j];
sl[j]=(sl_a-1)*n+sl_b;
if(j==n-1)
cout<<sl[j]<<endl;
else
cout<<sl[j]<<" ";
}
}
return 0;

}