c++九宫图小程序

软件和网站开发以及相关技术探讨
回复
greenhill
帖子: 27
注册时间: 2006-12-01 0:36

c++九宫图小程序

#1

帖子 greenhill » 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;

}
myhrvod
帖子: 7
注册时间: 2006-09-01 12:41
联系:

#2

帖子 myhrvod » 2008-07-15 22:25

好像有错误吧!
头像
lovewine
帖子: 1233
注册时间: 2006-03-25 10:36
联系:

#3

帖子 lovewine » 2008-07-16 13:14

如此简单吗?不敢相信啊
让自由的声音传传传传传传传传天下。。。
Spread the words of freedom.
头像
乾水居士
帖子: 54
注册时间: 2008-08-04 20:10
联系:

#4

帖子 乾水居士 » 2008-08-16 15:34

楼主奇才!完全无误!
上次由 乾水居士 在 2008-08-16 19:10,总共编辑 1 次。
我的另一个帐号:UWLinux
dlwdgl
帖子: 1
注册时间: 2008-01-20 14:15

#5

帖子 dlwdgl » 2008-08-16 16:01

int sl[n][n];
貌似c++里数组的初始化不能用变量的吧。java可以用int型变量 8)
greenhill
帖子: 27
注册时间: 2006-12-01 0:36

Re: c++九宫图小程序

#6

帖子 greenhill » 2013-08-04 17:24

谁来完善双数宫图
头像
月下叹逍遥
论坛版主
帖子: 33994
注册时间: 2010-10-07 14:23
系统: Archdows10
来自: 某系某星某洲某国某省某市
联系:

Re: c++九宫图小程序

#7

帖子 月下叹逍遥 » 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版
浮生七十今三十,从此凄惶未可知
johnnr
帖子: 40
注册时间: 2013-02-19 6:28

Re:

#8

帖子 johnnr » 2013-11-26 10:05

乾水居士 写了:楼主奇才!完全无误!
试了,没问题。虽然不懂。
附件
nine.JPG
greenhill
帖子: 27
注册时间: 2006-12-01 0:36

Re: c++九宫图小程序

#9

帖子 greenhill » 2020-09-16 15:36

#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;

}
回复