mfp吧 关注:779贴子:4,999
  • 3回复贴,共1

填数字游戏穷举解法

只看楼主收藏回复

//填1—9的数字,使式子成立
// □ □
// × □
// ————
// □ □
// + □ □
// ————
// □ □
function tianshuzi()
variable a,x
a=alloc_array(10)
for variable n=1 to 4
a[1]=n
for variable n=1 to 9
a[2]=n
if testok(a,2)!=1
continue
endif
for variable n=2 to 8
a[3]=n
if testok(a,3)!=1
continue
endif
x=(a[1]*10+a[2])*a[3]
if x<100
a[4]=floor(x/10)
a[5]=mod(x,10)
if or(testok(a,4)!=1,testok(a,5)!=1)
continue
else
for variable n=1 to 9
a[6]=n
if testok(a,6)!=1
continue
endif
for variable n=1 to 9
a[7]=n
if testok(a,7)!=1
continue
endif
x=10*(a[4]+a[6])+a[5]+a[7]
if x<100
a[8]=floor(x/10)
a[9]=mod(x,10)
if or(testok(a,8)!=1,testok(a,9)!=1)
continue
else
printf("%d%d×%d=%d%d,+%d%d=%d%d\n", _
a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9])
endif
endif
next
next
endif
endif
next
next
next
endf
function testok(a,n)
if a[n]==0
return 0
endif
for variable k=1 to n-1
if a[k]==a[n]
return 0
endif
next
return 1
endf


IP属地:浙江来自手机贴吧1楼2017-03-24 14:10回复
    数字不可重复


    IP属地:浙江来自手机贴吧2楼2017-03-24 14:13
    收起回复