新手问题 大神啊,为什么调用两次 ruby 函数,结果会互相干扰??

zhiaihh · 2014年04月02日 · 最后由 zhiaihh 回复于 2014年04月03日 · 2144 次阅读

$sz1 = [[""], [""], ["1", "7"]],[[""], [""], ["4", "2"]],[["3"], [""], [""]],[[""], [""], ["6"]],[[""], ["5"], [""]],[[""], [""], ["6"]],[[""], [""], ["1", "7"]],[["8"], [""], [""]],[[""], ["9"], [""]],[[""], ["10"], [""]],[[""], [""], ["*"]] i=0

while i<$sz1.size do p $sz1[i] i = i + 1 end puts

def read(p,temp1,temp5) temp1.push(p.to_s)#进队列 while !temp1.empty? do t=temp1.shift#.shift出队列 if !temp5.include?(t)&& t!="*" temp1=temp1+ $sz1[t.to_i][2] #合并数组,即进队列 temp5.push(t) #单个数据进队列 end end return temp5.sort #.uniq 去掉重复元素 end

def read2(p,temp2,temp3,temp4) #先把能得到的起始点找出 while !temp2.empty? do t=temp2.shift #.shift 出队列

if $sz1[t.to_i][p][0]!="*" temp3.push($sz1[t.to_i][p][0]) #进队列 end end

while !temp3.empty? do t=temp3.shift#.shift出队列 if !temp4.include?(t)&& t!="*" temp3=temp3+ $sz1[t.to_i][2] #合并数组,即进队列 temp4.push(t) #单个数据进队列 end end return temp4.sort end

#初始化数组 temp1=Array.new #接下来要读的数,队列 temp2=Array.new #已经读过的数 temp3=Array.new #已经读过的数 temp4=Array.new #已经读过的数 temp5=Array.new #已经读过的数

temp2=read 0,temp1,temp5 p temp2 p read2 1,temp2,temp3,temp4 temp2=read 0,temp1,temp5 p temp2 p read2 0,temp2,temp3,temp4

执行: temp2=read 0,temp1,temp5 p temp2 p read2 1,temp2,temp3,temp4 temp2=read 0,temp1,temp5 p temp2 p read2 0,temp2,temp3,temp4 结果: [[""], [""], ["1", "7"]] [[""], [""], ["4", "2"]] [["3"], [""], [""]] [[""], [""], ["6"]] [[""], ["5"], [""]] [[""], [""], ["6"]] [[""], [""], ["1", "7"]] [["8"], [""], [""]] [[""], ["9"], [""]] [[""], ["10"], [""]] [[""], [""], ["*"]]

["0", "1", "2", "4", "7"] ["1", "2", "4", "5", "6", "7"] ["0", "1", "2", "4", "7"] ["1", "2", "3", "4", "5", "6", "7", "8"]

输出结果应该不一样啊

执行: temp2=read 0,temp1,temp5 p temp2 p read2 1,temp2,temp3,temp4 结果: [[""], [""], ["1", "7"]] [[""], [""], ["4", "2"]] [["3"], [""], [""]] [[""], [""], ["6"]] [[""], ["5"], [""]] [[""], [""], ["6"]] [[""], [""], ["1", "7"]] [["8"], [""], [""]] [[""], ["9"], [""]] [[""], ["10"], [""]] [[""], [""], ["*"]]

["0", "1", "2", "4", "7"] ["1", "2", "4", "5", "6", "7"]

执行: temp2=read 0,temp1,temp5 p temp2 p read2 0,temp2,temp3,temp4 结果: [[""], [""], ["1", "7"]] [[""], [""], ["4", "2"]] [["3"], [""], [""]] [[""], [""], ["6"]] [[""], ["5"], [""]] [[""], [""], ["6"]] [[""], [""], ["1", "7"]] [["8"], [""], [""]] [[""], ["9"], [""]] [[""], ["10"], [""]] [[""], [""], ["*"]]

["0", "1", "2", "4", "7"] ["1", "2", "3", "4", "6", "7", "8"]

#多了个 5!!!!!还有,,,其他问题,总之为什么会互相干扰?????

这代码排版,用 markdown 排一下版吧

#2 楼 @Martin91 你好,请问为什么调用两次函数结果会和分两次单次执行的结果不一样,markdown 代码我稍后贴上

排版后再给你看,提问还请多考虑一下义务过来解答人的体验。

###字体不知道怎么调大,只有各位大大自己浏览器调了,感谢各位指点

$sz1 = [["*"], ["*"], ["1", "7"]],[["*"], ["*"], ["4", "2"]],[["3"], ["*"], ["*"]],[["*"], ["*"], ["6"]],[["*"], ["5"], ["*"]],[["*"], ["*"], ["6"]],[["*"], ["*"], ["1", "7"]],[["8"], ["*"], ["*"]],[["*"], ["9"], ["*"]],[["*"], ["10"], ["*"]],[["*"], ["*"], ["*"]]
i=0

while i<$sz1.size do
  p $sz1[i]
  i = i + 1
end
puts

def read(p,temp1,temp5)
  temp1.push(p.to_s)#进队列
  while !temp1.empty? do
    t=temp1.shift#.shift出队列
    if !temp5.include?(t)&& t!="*"
      temp1=temp1+ $sz1[t.to_i][2] #合并数组,即进队列
      temp5.push(t) #单个数据进队列
    end
  end
  return temp5.sort #.uniq去掉重复元素
end

def read2(p,temp2,temp3,temp4)
#先把能得到的起始点找出
  while !temp2.empty? do
    t=temp2.shift #.shift出队列

    if $sz1[t.to_i][p][0]!="*" 
      temp3.push($sz1[t.to_i][p][0]) #进队列
    end
  end

  while !temp3.empty? do
    t=temp3.shift#.shift出队列
    if !temp4.include?(t)&& t!="*"
      temp3=temp3+ $sz1[t.to_i][2] #合并数组,即进队列
      temp4.push(t) #单个数据进队列
    end
  end
  return temp4.sort
end

#初始化数组
temp1=Array.new #接下来要读的数,队列
temp2=Array.new #已经读过的数
temp3=Array.new #已经读过的数
temp4=Array.new #已经读过的数
temp5=Array.new #已经读过的数

temp2=read 0,temp1,temp5
p temp2
p read2 1,temp2,temp3,temp4
temp2=read 0,temp1,temp5
p temp2
p read2 0,temp2,temp3,temp4

我解决了,用数组的 clear 来清除影响

问题就出在数组上

#1 楼 @zhiaihh 之所以会多了个 5,是因为你第二次调用read2的时候,temp4 跟第一次调用read2方法时的 temp4 不一样,你可以试着在第二次调用read2之前重置一下 temp4 为空的数组,这个时候应该就能得到预期结果了。PS:我没有仔细研究你的算法,只是猜测并试了一下,你可以从 temp4 作为切入口。 另外提几个小建议:

  1. 变量名还是不用$开头了吧,感觉像是 javascript 的习惯(这个纯粹建议);
  2. 数据遍历可以直接调用each方法,没必要做迭代器的判断: ruby # use each to traversal the array $sz1.each {|ele| p ele}
需要 登录 后方可回复, 如果你还没有账号请 注册新账号