主题:魔方阵的解法,奇数阶和偶数阶。
有事找我请烧钱
[专家分:500] 发布于 2004-04-13 21:15:00
http://163.20.91.130/sct/content/1972/00100034/0014.htm
这里有魔方阵的演算方法,C++代码见后帖~~~
___
今天写出了阶数是4的倍数的C++代码(其实C也一样可用),完全根据上面的那篇文章的方法写出,代码如果看不明白的话就看那篇文章.
阶数是4K+2(K为整数)的解法正在思考中....
11 楼
zhadalou [专家分:480] 发布于 2004-07-13 23:45:00
我用vfp做的,可以完成任意阶
vfp98源程序,在此凑个热闹:
clear all
close all
set sysmenu to
set decimals to
on error =error()
set proc to mf.prg additive
on error
formset1=createobject('formset1')
_screen.addobject('commtc','commtc')
_screen.commtc.left=-100
on shutdown clear events
set cursor on
_screen.caption='魔方'
systool=createobject('_systoolbars')&&创建系统工具栏
systool.hidesystemtoolbars&&隐藏系统工具栏
read events
_screen.caption='Mcrosoft Visual FoxPro'
set sysmenu to default
use
dele file mfk.dbf
wait clear
close all
on error
on shutdown
*clear all
set proc to
retu
proc szsy
use
delete file mfk.dbf
wait clear
release n,f,d,m,fb
messagebox('数字上溢,请重建魔方')
retu to master
define class formset1 as formset
proc init
this.addobject('toolbar1','toolbar1')
this.toolbar1.Dock[0,0,0]
this.toolbar1.move(0)&&当常用工具栏已经启用时,此工具栏仍然可见
this.toolbar1.show
enddefine
define class toolbar1 as toolbar
showtips=.t.
controlbox=.f.
top=-1
add object commjmf as commjmf
add object textjs as textjs
add object textsx as textsx
add object textgc as textgc
add object sep1 as separator
add object commys as commys
add object texth as texth
add object textl as textl
add object sep2 as separator
add object commjsq as commjsq
add object sep3 as separator
add object commtc as commtc
enddefine
define class commjsq as commandbutton
width=40
height=20
caption='计算器'
visible=.t.
proc click
activate window calculator
move window calculator to 0,68
enddefine
define class commys as commandbutton
width=40
height=20
caption='验证'
visible=.t.
proc when
use
proc click
if file('mfk.dbf')
formset1.toolbar1.texth.enabled=.t.
formset1.toolbar1.texth.setfocus
endif
enddefine
define class texth as textbox
enabled=.f.
inputmask='999xx'
tooltiptext='验证取行数据'
width=50
proc init
this.value=' 行'
proc keypress
para nk,ns
if nk=13
h=val(subs(this.value,1,3))
do case
case n/4=int(n/4)
if h>1 and h<=n+1
this.enabled=.f.
formset1.toolbar1.textl.enabled=.t.
formset1.toolbar1.textl.setfocus
else
messagebox('请重新输入')
this.value=' 行'
endif
othe
if h>0 and h<=n
this.enabled=.f.
formset1.toolbar1.textl.enabled=.t.
formset1.toolbar1.textl.setfocus
else
messagebox('请重新输入')
this.value=' 行'
endif
endc
endif
enddefine
define class textl as textbox
enabled=.f.
inputmask='999xx'
tooltiptext='验证取列数据'
width=50
proc init
this.value=' 列'
proc keypress
para nk,ns
if nk=13
l=val(subs(this.value,1,3))
do case
case n/4=int(n/4)
if l>1 and l<=n+1
this.enabled=.f.
do yanzen
else
messagebox('请重新输入')
this.value=' 列'
endif
othe
if l>0 and l<=n
this.enabled=.f.
do yanzen
else
messagebox('请重新输入')
this.value=' 列'
endif
endc
endif
enddefine
define class textjs as textbox
inputmask='9999999'
enabled=.f.
tooltiptext='输入魔方阶数,请勿超过255'
width=50
proc init
this.value=' 阶数'
proc when
this.value=''
proc keypress
para nk,ns
if nk=13
xs=val(subs(this.value,1,3))
if xs<3 or xs>254
messagebox('阶数应>=3,<=254,请重新输入')
this.value=' 阶数'
else
this.enabled=.f.
formset1.toolbar1.textsx.enabled=.t.
formset1.toolbar1.textsx.setfocus
endif
endif
enddefine
define class textsx as textbox
width=50
inputmask='99999999'
enabled=.f.
tooltiptext='魔方首项'
proc init
this.value=' 首项'
proc when
this.value=''
proc keypress
para nk,ns
if nk=13
this.enabled=.f.
formset1.toolbar1.textgc.enabled=.t.
formset1.toolbar1.textgc.setfocus
endif
enddefine
define class textgc as textbox
width=50
inputmask='999999'
enabled=.f.
tooltiptext='魔方公差'
proc init
this.value=' 公差'
proc when
this.value=''
proc keypress
para nk,ns
if nk=13
this.enabled=.f.
_screen.commtc.setfocus
formset1.toolbar1.commtc.setfocus
do jmf
formset1.toolbar1.textjs.value=' 阶数'
formset1.toolbar1.textsx.value=' 首项'
formset1.toolbar1.textgc.value=' 公差'
endif
enddefine
define class commtc as commandbutton
width=40
height=20
caption='\<Q 退出'
visible=.t.
proc click
use
clear events
enddefine
define class commjmf as commandbutton
width=40
height=20
caption='建魔方'
visible=.t.
proc click
wait clear
use
dele file mfk.dbf
formset1.toolbar1.textjs.enabled=.t.
formset1.toolbar1.textjs.setfocus
enddefine
proc jmf
release n,f,d,m,fb
public n,f,d,m,fb
use
dele file mfk.dbf
clea
n=val(alltrim(formset1.toolbar1.textjs.value))
f=val(alltrim(formset1.toolbar1.textsx.value))
d=val(alltrim(formset1.toolbar1.textgc.value))
if n/4=int(n/4)
m=n+2
zd='j1 n(5)'
for i=2 to m
if i<10
zd=zd+',j'+str(i,1)+' n(5)'
endif
if i>9.and.i<100
zd=zd+',j'+str(i,2)+' n(5)'
endif
if i>99
zd=zd+',j'+str(i,3)+' n(5)'
endif
endfor
else
zd='j1 n(5)'
for i=2 to n
if i<10
zd=zd+',j'+str(i,1)+' n(5)'
endif
if i>9.and.i<100
zd=zd+',j'+str(i,2)+' n(5)'
endif
if i>99
zd=zd+',j'+str(i,3)+' n(5)'
endif
endfor
endif
create dbf mfk(&zd.)
use mfk
if n/4=int(n/4)
for i=1 to m
appe blank
endfor
else
for i=1 to n
appe blank
endfor
endif
do case
case n/2<>int(n/2)
do jsmf&& with n,f,d
case n/4=int(n/4)
fb=f-d
do sbmf with m,d,fb
othe
do tww&& with n,f,d
fb=f+9*d
if n>6
do jtww&& with n,f,d,fb
endif
do sbmf with n,d,fb
endc
clea
wait'魔方已建成,请一览' window at 33,10 nowait noclear
go top
brow nomodify
use
wait clear
retu
proc yanzen
wait clear
clear
release h,l
public h,l
h=val(formset1.toolbar1.texth.value)
l=val(formset1.toolbar1.textl.value)
use
use mfk
yzz3=0
yzz4=0
if n/4=int(n/4)
go 2
for j=2 to n+1
yzz=field(j)
yzz3=yzz3+&yzz
skip
endfor
go 2
for j=n+1 to 2 step -1
yzz=field(j)
yzz4=yzz4+&yzz
skip
endfor
else
go 1
for j=1 to n
yzz=field(j)
yzz3=yzz3+&yzz
skip
endfor
go 1
for j=n to 1 step -1
yzz=field(j)
yzz4=yzz4+&yzz
skip
endfor
endif
go h
yzz2=0
if n/4=int(n/4)
for j=1 to m
yzz=field(j)
yzz2=yzz2+&yzz
endfor
else
for j=1 to n
yzz=field(j)
yzz2=yzz2+&yzz
endfor
endif
zd=field(l)
sum &zd to yzz1
clear
go top
if yzz4=yzz3 and yzz3=yzz2 and yzz2=yzz1
ycwm=' 正确无误'
else
ycwm=' 有错误'
endif
wait'上对角线之和='+ltrim(str(yzz4,7))+chr(13)+'下对角线之和='+ltrim(str(yzz3,7));
+chr(13)+'第'+str(h,3)+' 行之和='+ltrim(str(yzz2,7))+chr(13)+;
'第'+str(l,3)+' 列之和='+ltrim(str(yzz1,7))+ycwm;
window at 33,10 nowait noclear
brow nomodify
wait clear
use
retu
proc jtww&&加填外围
i=5
j=5
m=2*f+d*n*n-d
do whil j<n-1
go 1
zd=field(j+1)
repl &zd with fb+d
zd=field(j+4)
repl &zd with fb+4*d
zd=field(j+3)
repl &zd with m-fb-2*d
zd=field(j+2)
repl &zd with m-fb-3*d
go I+1
zd=field(1)
repl &zd with fb+8*d
zd=field(n)
repl &zd with m-fb-8*d
skip
zd=field(n)
repl &zd with fb+6*d
zd=field(1)
repl &zd with m-fb-6*d
skip
zd=field(n)
repl &zd with fb+7*d
zd=field(1)
repl &zd with m-fb-7*d
skip
zd=field(1)
repl &zd with fb+5*d
zd=field(n)
repl &zd with m-fb-5*d
go n
zd=field(j+3)
repl &zd with fb+2*d
zd=field(j+2)
repl &zd with fb+3*d
zd=field(j+1)
repl &zd with m-fb-d
zd=field(j+4)
repl &zd with m-fb-4*d
fb=fb+8*d
i=i+4
j=j+4
enddo
retu
proc tww&&填外围
*para n,f,d
on error do szsy
m=f+d*(n*n-1)
go 1
zd=field(1)
repl &zd with f+8*d
zd=field(2)
repl &zd with m-5*d
zd=field(3)
repl &zd with m-6*d
zd=field(4)
repl &zd with m-7*d
zd=field(5)
repl &zd with f+d
zd=field(n)
repl &zd with f+9*d
skip
zd=field(1)
repl &zd with m
zd=field(n)
repl &zd with f
skip
zd=field(1)
repl &zd with m-4*d
zd=field(n)
repl &zd with f+4*d
skip
zd=field(1)
repl &zd with f+2*d
zd=field(n)
repl &zd with m-2*d
skip
zd=field(1)
repl &zd with f+3*d
zd=field(n)
repl &zd with m-3*d
go n
zd=field(1)
repl &zd with m-9*d
zd=field(2)
repl &zd with f+5*d
zd=field(3)
repl &zd with f+6*d
zd=field(4)
repl &zd with f+7*d
zd=field(5)
repl &zd with m-d
zd=field(n)
repl &zd with m-8*d
on error
retu
proc sbmf&&四倍魔方
parameter n,d,fb
on error do szsy
for i=2 to n-1
for j=2 to n-1
if int((j+1)/4)=(j+1)/4.or.int(j/4)=j/4
ia=n-i+1
else
ia=i
endif
fb=fb+d
go ia
zd=field(j)
if int((i+1)/4)=(i+1)/4.or.int(i/4)=i/4
repl &zd with fb+d*(n-2*j+1)
else
repl &zd with fb
endif
endfor
endfor
on error
retu
proc jsmf&&奇数魔方
*para n,f,d
on error do szsy
i=(n+3)/2
j=(n+1)/2
go i
zd=field(j)
repl &zd with f
l=f+d*(n*n-1)
f=f+d
for p=f to l step d
i=i+1
j=j+1
if i>n.and.j>n
i=2
j=n
endif
if i>n
i=1
endif
if j>n
j=1
endif
go i
zd=field(j)
if &zd<>0
i=i+1
j=j-1
if i>n
i=1
endif
endif
go i
zd=field(j)
repl &zd with p
endfor
clea
on error
retu
define class _systoolbars as custom
name='_systoolbars'
parentclass='_custom'
class='_systoolbars'
classlibrary='c:\vfp98\ffc\_base.vcx'
height=22
width=24
builderx=HOME()+"Wizards\BuilderD,BuilderDForm"
csetobjrefprogram=IIF(VERSION(2)=0,"",HOME()+"FFC\")+"SetObjRf.prg"
proc destroy
DODEFAULT()
IF lAutomatic=.f.
this.ShowSystemToolbars
ENDIF
proc init
LPARAMETERS tlAuto
IF NOT DODEFAULT()
RETURN .F.
ENDIF
THIS.InitializeToolbarArray()
IF lAutomatic OR tlAuto
lAutomatic = .T.
this.HideSystemToolbars()
ENDIF
proc hidesystemtoolbars&&&&隐藏系统工具栏
LOCAL iIndex
FOR iIndex = 1 TO ALEN(aSystemToolbars,1)
IF WEXIST(aSystemToolbars[iIndex,1]) AND WVISIBLE(aSystemToolbars[iIndex,1])
aSystemToolbars[iIndex,2] = .T.
HIDE WINDOW (aSystemToolbars[iIndex,1])
ENDIF
ENDFOR
proc initializetoolbararray
***********************************************************
* System Toolbar subset from FOXPRO.H, Tastrade STRINGS.H
*-- Toolbar names
#DEFINE TB_FORMDESIGNER_LOC "表单设计器"
#DEFINE TB_STANDARD_LOC "常用"
#DEFINE TB_LAYOUT_LOC "布局"
#DEFINE TB_QUERY_LOC "查询设计器"
#DEFINE TB_VIEWDESIGNER_LOC "视图设计器"
#DEFINE TB_COLORPALETTE_LOC "调色板"
#DEFINE TB_FORMCONTROLS_LOC "表单控件"
#DEFINE TB_DATADESIGNER_LOC "数据库设计器"
#DEFINE TB_REPODESIGNER_LOC "报表设计器"
#DEFINE TB_REPOCONTROLS_LOC "报表控件"
#DEFINE TB_PRINTPREVIEW_LOC "打印预览"
public aSystemToolbars[11,2],lAutomatic
DIME aSystemToolbars[11,2]
aSystemToolbars[1,1]=TB_STANDARD_LOC
aSystemToolbars[2,1]=TB_LAYOUT_LOC
aSystemToolbars[3,1]=TB_QUERY_LOC
aSystemToolbars[4,1]=TB_VIEWDESIGNER_LOC
aSystemToolbars[5,1]=TB_COLORPALETTE_LOC
aSystemToolbars[6,1]=TB_FORMCONTROLS_LOC
aSystemToolbars[7,1]=TB_DATADESIGNER_LOC
aSystemToolbars[8,1]=TB_REPODESIGNER_LOC
aSystemToolbars[9,1]=TB_REPOCONTROLS_LOC
aSystemToolbars[10,1]=TB_PRINTPREVIEW_LOC
aSystemToolbars[11,1]=TB_FORMDESIGNER_LOC
proc showsystemtoolbars&&&&显示系统工具栏
LOCAL lcCustomToolbar, iIndex
FOR iIndex = 1 TO ALEN(aSystemToolbars,1)
IF WEXIST(aSystemToolbars[iIndex,1]) AND aSystemToolbars[iIndex,2]
if (aSystemToolbars[iIndex,1])=TB_STANDARD_LOC
SHOW WINDOW (aSystemToolbars[iIndex,1]) same
move window (aSystemToolbars[iIndex,1]) to 0,0
else
SHOW WINDOW (aSystemToolbars[iIndex,1])
* move window (aSystemToolbars[iIndex,1]) to 20,0
endif
ENDIF
ENDFOR
enddefine