A number is called 'desirable' if all thedigits are strictly ascending eg: 159 as 1<5<9. You know that your rivalhas a strictly numeric password that is 'desirable'. Your close ally has givenyou the number of digits (N) in your rival's password. WAP th\hjtat takes in'N' as input and prints out all possible 'desirable' numbers that can be formedwith N digits.

递归:参数记录剩余需要生成的长度和最小的能append的数字

def bfs(remain,start,string)
if remain == 0
@ans << string
else
(start..9).each {|i| bfs(remain-1, i+1, string + i.to_s)}
end
end def desire_number(n)
@ans = []
bfs(n,1,'')
@ans
end

循环:用两个数组分别保存i-1的情况和i的情况 i from 1 to n

def desire_number(n)
 return 0 if n == 0
a = ['']
(n-1).times do
b = []
a.each {|x| (x[-1].to_i..9).each{|y| b << x+y.to_s}}
a = b
end
a
end

最新文章

  1. 关于 Word Splitting 和 IFS 的三个细节
  2. Windows-006-映射网络驱动器图文详解
  3. android设置软键盘搜索键以及监听搜索键点击时发生两次事件的问题解决
  4. 简单的SocketExample
  5. 开源的读取Excel文件组件-ExcelDataReader
  6. lua之mysql编程
  7. JS0热身运动
  8. IO流+数据库课后习题
  9. HTML5 音频视频
  10. Qt实现不同Treewidget之间拖拽
  11. webdynpro MESSGAE
  12. char与unsigned char 差别
  13. Vowel Counting
  14. Oracle 不小心删除undo数据文件以及磁盘空间不足导致不能登录的解决办法
  15. js写插件教程入门
  16. 文档大师 在Win10 IE11下,文档集画面无法正常显示Word等Office文档的解决方法
  17. Linq多表联合查询,在View中绑定数据
  18. MySQL缓存命中率概述
  19. java中 列表,集合,数组之间的转换
  20. MongoDB学习笔记(5)--document

热门文章

  1. IntelliJ IDEA优化总结
  2. ubuntu eclipse下配置C++ 环境
  3. UIActivityIndicatorView添加到UIButton上并响应事件
  4. 访问修饰符internal
  5. What is a Windows USB device path and how is it formatted?
  6. div里面的内容超出自身高度时,显示省略号
  7. Pointer arithmetic for void pointer in C
  8. Topology的构建
  9. NPOI格式设置
  10. 你应该知道的jQuery技巧