设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索

新人问个问题,生成3个随机数字abc,并把他们排序。

查看数: 1749 | 评论数: 8 | 收藏 0
关灯 | 提示:支持键盘翻页<-左 右->
    组图打开中,请稍候......
发布时间: 2014-2-6 20:21

正文摘要:

恩,就是这样。

回复

fux2 发表于 2014-2-7 19:08:48
  1. a,b,c=rand(100),rand(100),rand(100)
  2. p a,b,c
  3. a,b,c=*[a,b,c].sort
  4. p a,b,c
复制代码
这么简单的事情为什么楼上的代码都那么长……
铃仙·优昙华院·因幡 发表于 2014-2-7 12:59:41
RGSS_2 发表于 2014-2-7 12:56
e = [rand(100), rand(100),rand(100)]
   p  [e[0],e[1],e[2]] if e[0]>e[1] and e[0]>e[2] and e[1] ...

你这种写法连排序都算不上. 看看百科吧.

http://baike.baidu.com/link?url= ... IJvq-0cEA3EOpUrk6Rt
RGSS_2 发表于 2014-2-7 12:56:15
RGSS_2 发表于 2014-2-7 12:50
自己想了半天,就想出了这个。可我发现了个问题, 这是次排序的数量还好是3个,要是30个岂不是麻烦死?{: ...

   e = [rand(100), rand(100),rand(100)]
   p  [e[0],e[1],e[2]] if e[0]>e[1] and e[0]>e[2] and e[1]>e[2]
   p  [e[0],e[2],e[1]] if e[0]>e[1] and e[0]>e[2] and e[2]>e[1]
   
   p  [e[1],e[0],e[2]] if e[1]>e[0] and e[1]>e[2] and e[0]>e[2]
   p  [e[1],e[2],e[0]] if e[1]>e[0] and e[1]>e[2] and e[2]>e[0]
   
   p  [e[2],e[0],e[1]] if e[2]>e[0] and e[2]>e[1] and e[0]>e[1]
   p  [e[2],e[1],e[0]] if e[2]>e[0] and e[2]>e[1] and e[1]>e[0]
这样应该比刚刚简单多。

评分

参与人数 1星屑 -10 收起 理由
铃仙·优昙华院·因幡 -10 禁止连贴

查看全部评分

RGSS_2 发表于 2014-2-7 12:50:11
铃仙·优昙华院·因幡 发表于 2014-2-7 12:25
得到的就是排好顺序的数字了.

如果你想获取的是一串数字里最大或者最小的那个, 也可以用默认的方法来解决. ...
  1. def m (a)
  2.    d = a   
  3.    return d
  4.   end
  5. mer = m   (rand(100) ).to_s
  6.   p mer
  7.   
  8. def ma(b)
  9.    d = b  
  10.    return d
  11.   end
  12. maer = ma  (rand(100) ).to_s
  13.   p maer
  14.   
  15.    def max(c)
  16.    d = c   
  17.    return d
  18.   end
  19. maxer = max(rand(100) ).to_s
  20.   p maxer
  21.   
  22.   e = [mer, maer,maxer]
  23.    p  [e[0],e[1],e[2]] if e[0]>e[1] and e[0]>e[2] and e[1]>e[2]
  24.    p  [e[0],e[2],e[1]] if e[0]>e[1] and e[0]>e[2] and e[2]>e[1]
  25.    
  26.    p  [e[1],e[0],e[2]] if e[1]>e[0] and e[1]>e[2] and e[0]>e[2]
  27.    p  [e[1],e[2],e[0]] if e[1]>e[0] and e[1]>e[2] and e[2]>e[0]
  28.    
  29.    p  [e[2],e[0],e[1]] if e[2]>e[0] and e[2]>e[1] and e[0]>e[1]
  30.    p  [e[2],e[1],e[0]] if e[2]>e[0] and e[2]>e[1] and e[1]>e[0]
复制代码
自己想了半天,就想出了这个。可我发现了个问题, 这是次排序的数量还好是3个,要是30个岂不是麻烦死?{:2_264:}新人刚学脚本,你那个我看的不是特别懂(我自己太笨)。
铃仙·优昙华院·因幡 发表于 2014-2-7 12:25:20

  1. a = []
  2. 0...3.times do |number|
  3.   a << rand(100)
  4. end
  5. p a.sort
复制代码
得到的就是排好顺序的数字了.

如果你想获取的是一串数字里最大或者最小的那个, 也可以用默认的方法来解决.

  1. a = []
  2. 0...3.times do |number|
  3.   a << rand(100)
  4. end
  5. p a.max # 获取最大的
  6. p a.min # 获取最小的
复制代码
再或者不想用默认的方法的话, 也可以借用 冒泡排序 的算法来解决.

  1. a = []
  2. 0...3.times do |number|
  3.   a << rand(100)
  4. end
  5. max_number = 0
  6. for i in a
  7.   max_number = i if max_number < i
  8. end
  9. p max_number
复制代码

评分

参与人数 1梦石 +1 收起 理由
怪蜀黍 + 1 认可答案

查看全部评分

SuperMario 发表于 2014-2-7 00:49:03
方法可以返回3个参数的,就是一个数组。

点评

求说的仔细点啊,好吗  发表于 2014-2-7 11:50
RGSS_2 发表于 2014-2-6 20:51:05
我刚学,不怎么会额。
拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2025-6-14 08:26

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表