用关联下拉框方法解决“称三次从12球中找出唯一但不知轻重的球”

qiushuiwuhen 2002-06-09 09:39:23
原题参见 http://www.csdn.net/news/newspl/5/5268.shtml

昨天写了初稿试验通过,今天改成关联下拉,
多级关联的算法见 http://www.csdn.net/expert/Topicview1.asp?id=723099
现在赋予完整的程序应用,代码如下


在12个小球中有一个和其他不同(或轻或重),现在先拿起笔,记下哪个球重或者轻,然后继续下面的游戏<br>
<select name=one></select>
<select name=two></select>
<select name=three></select>
<select name=four></select>

<script>
var arrSel=["one","two","three","four"];//arrSel定义了要修改的下拉框
var i=0,arrData=[];//arrData中搁的是数据,每条都显示各级的数据
arrData[i++]=["(1,2,3,4)比(5,6,7,8)","请先做前面的选择","请先做前面的选择","请先做前面的选择"]
arrData[i++]=["重","(1,2,5)比(3,6,9)","请先做前面的选择","请先做前面的选择"]
arrData[i++]=["重","重","(1)比(2)","请先做前面的选择"]
arrData[i++]=["重","重","重","1球比较重"]
arrData[i++]=["重","重","平","6球比较轻"]
arrData[i++]=["重","重","轻","2球比较重"]
arrData[i++]=["重","平","(7)比(8)","请先做前面的选择"]
arrData[i++]=["重","平","重","8球比较轻"]
arrData[i++]=["重","平","平","4球比较重"]
arrData[i++]=["重","平","轻","7球比较轻"]
arrData[i++]=["重","轻","(1)比(3)","请先做前面的选择"]
arrData[i++]=["重","轻","重","无解"]
arrData[i++]=["重","轻","平","5球比较轻"]
arrData[i++]=["重","轻","轻","3球比较重"]
arrData[i++]=["平","(9,10)比(1,11)","请先做前面的选择","请先做前面的选择"]
arrData[i++]=["平","重","(9)比(10)","请先做前面的选择"]
arrData[i++]=["平","重","重","9球比较重"]
arrData[i++]=["平","重","平","10球比较重"]
arrData[i++]=["平","重","轻","11球比较轻"]
arrData[i++]=["平","平","(1)比(12)","请先做前面的选择"]
arrData[i++]=["平","平","重","12球比较轻"]
arrData[i++]=["平","平","平","无解"]
arrData[i++]=["平","平","轻","12球比较重"]
arrData[i++]=["平","轻","(9)比(10)","请先做前面的选择"]
arrData[i++]=["平","轻","重","9球比较轻"]
arrData[i++]=["平","轻","平","10球比较轻"]
arrData[i++]=["平","轻","轻","11球比较重"]
arrData[i++]=["轻","(1,2,5)比(3,6,9)","请先做前面的选择","请先做前面的选择"]
arrData[i++]=["轻","重","(1)比(3)","请先做前面的选择"]
arrData[i++]=["轻","重","重","3球比较轻"]
arrData[i++]=["轻","重","平","5球比较重"]
arrData[i++]=["轻","重","轻","无解"]
arrData[i++]=["轻","平","(7)比(8)","请先做前面的选择"]
arrData[i++]=["轻","平","重","7球比较重"]
arrData[i++]=["轻","平","平","4球比较轻"]
arrData[i++]=["轻","平","轻","8球比较重"]
arrData[i++]=["轻","轻","(1)比(2)","请先做前面的选择"]
arrData[i++]=["轻","轻","重","2球比较轻"]
arrData[i++]=["轻","轻","平","6球比较重"]
arrData[i++]=["轻","轻","轻","1球比较轻"]
</script>
<script>
function qswhInit(num){
/******* by qiushuiwuhen(2002-5-15) ********/
var i,j,arrTemp=[];
for(i=0;i<num;i++)arrTemp[i]=document.all(arrSel[i]).options[document.all(arrSel[i]).selectedIndex].text
if(num==arrSel.length){//这里处理最后的数据。
return;
}
with(document.all(arrSel[num])){
length=0
for(i=0;i<arrData.length;i++){
for(j=0;j<num;j++)if(arrTemp[j]!=arrData[i][j])break;
if(j!=num)continue;
if(length==0||options[length-1].text!=arrData[i][num])
options[length++].text=arrData[i][num];
}
onchange=new Function("qswhInit("+(num+1)+")");
onchange();
}
}
qswhInit(0);//初始化
</script>

请大家测试抓虫,呵呵

ps.好久没真正锻炼头脑了,挺舒服的感觉。
昨天下午还解决了 http://www.csdn.net/expert/Topicview1.asp?id=787573
思路和过程探测见 http://www.52bj.com/bbs/newsdetail.asp?id=508415
...全文
8 点赞 收藏 10
写回复
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiushuiwuhen 2002-12-13
修正:

arrData[i++]=["平","轻","(9)比(10)","请先做前面的选择"]
=>
arrData[i++]=["平","重","(9)比(11)","请先做前面的选择"]

arrData[i++]=["平","轻","(9)比(10)","请先做前面的选择"]
=>
arrData[i++]=["平","轻","(9)比(11)","请先做前面的选择"]

回复
superkiller 2002-12-13
这么多猩猩!!!
回复
emu 2002-12-13
半年都过去了你还记得回来修正啊?
回复
Andrawu 2002-06-12
good.
还有吗?
回复
qiushuiwuhen 2002-06-12
揭帖,每人10分
回复
qiushuiwuhen 2002-06-09
我在加班.......

忙里偷闲罢了
回复
possible_Y 2002-06-09
哈哈,好玩儿
回复
possible_Y 2002-06-09
呵呵,好玩儿
回复
wasy 2002-06-09
帅呆了
回复
emu 2002-06-09
你倒挺悠闲的。
听说有两种程序员有周末可度的
看来我不属于那两种,不知堪喜还是堪忧
回复
发动态
发帖子
JavaScript
创建于2007-09-28

8.4w+

社区成员

Web 开发 JavaScript
申请成为版主
社区公告
暂无公告