题目:
有下面两个数组
gColorList=["Red", "Yellow", "Green", "Blue", "White", "Purple", "Brown", "Pink", "Black", "Orange"]
gNumList=["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "Ten", "zero"]
请组合出 4 个 6 组 不重复的项目 如["Red","one"],["Yellow","two"],["Green","three"],["White","four"],["Brown","five"],["Blue","six"] 为一组。
["Red","one"]代表一个子项,同组中color, num 绝对不能一样。所有子项中不能重复相同,并且看起来是随机的,不是有某种规律的排列。
解答:
上述题目中涉及到了循环,组合,随机,列表操作等数学相关知识。可以通过下面的程序实现,程序供参考:(amith)
global gColorList
global gNumList
global gQuesList
on amith
gColorList=["Red", "Yellow", "Green", "Blue", "White", "Purple", "Brown", "Pink", "Black", "Orange"]
gNumList=["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "Ten", "zero"]
gQuesList=[]
repeat while gQuesList.count < 4 * 6
add gQuesList,buildit()
end repeat
put gQuesList
end
on buildit
tempList=[]
temp=getRandom(gColorList.count,6)
temp2=getRandom(gNumList.count,6)
repeat with i=1 to 6
add tempList,[gColorList[temp[i]],gNumList[temp2[i]]]
if getPos(gQuesList,[temp[i],temp2[i]]) then exit
end repeat
return tempList
end
-- 从 t1 (可以是列表也可以是一个比t2大的数)中随机取 t2 个数 的函数体
-- 如: getRandom(100,2)表示从1到100随机取2个数
-- getRandom([1,2,3,4],3) 表示从列表[1,2,3,4]随机取3个数
on getRandom t1,t2
if listp(t1) then
list1=t1
else
list1=[]
repeat with i=1 to t1
add list1,i
end repeat
end if
list2=[]
repeat with i=1 to t2
repeat while true
r=random(list1.count)
mm=list1[r]
if getOne(list2,mm) then next repeat
add list2,mm
exit repeat
end repeat
end repeat
return list2
end
更多讨论参考这里:http://www.aougu.net/bbs/read.php?topicid=2102
