# 智力题:3个5和1个1的四则运算如何使其结果为24
# 下面是tcl简单解决办法。没有玩过的不妨想想,然后再看程序实现。
#
set vlist "5.00 5.00 5.00 1.00"
set func "+ - * /"
set result 24
set possNo 0
set findout 0
# a b c d = 24 = 5*(5-1/5)
for {set i 0} {$i<4} {incr i} {
for {set j 0} {$j<4} {incr j} {
if {$i==$j} {continue}
for {set k 0} {$k<4} {incr k} {
if {$i==$k||$j==$k} {continue}
for {set l 0} {$l<4} {incr l} {
if {$i==$l||$j==$l||$k==$l} {continue}
set a [lindex $vlist $i]
set b [lindex $vlist $j]
set c [lindex $vlist $k]
set d [lindex $vlist $l]
foreach f1 $func {
foreach f2 $func {
foreach f3 $func {
set expl ""
# 0
lappend expl "$a $f1 $b $f2 $c $f3 $d"
# 1
lappend expl "($a $f1 $b) $f2 $c $f3 $d"
lappend expl "($a $f1 $b $f2 $c) $f3 $d"
lappend expl "$a $f1 ($b $f2 $c) $f3 $d"
lappend expl "$a $f1 ($b $f2 $c $f3 $d)"
lappend expl "$a $f1 $b $f2 ($c $f3 $d)"
# 2
lappend expl "($a $f1 $b) $f2 ($c $f3 $d)"
foreach exp $expl {
if {$findout} {break}
set exp [join $exp ""]
incr possNo
if {[catch {
if {[expr abs($exp-$result)]<=0.0001} {
puts "$possNo: $exp = [expr 1.00*$exp]"
set findout 1
break
}
} ierr]} {
}
}
if {$findout} {break}
}
if {$findout} {break}
}
if {$findout} {break}
}
if {$findout} {break}
}
if {$findout} {break}
}
if {$findout} {break}
}
if {$findout} {break}
}
我的日志
本文链接地址:http://lostCity.blog.zj.com/blog/d-161502.html
上一篇:[转帖]孔乙己----华为版
下一篇:[TCL]命令语法:upvar


TAG:
评分(