Epic - Desirable Number
2015-06-14 11:27
218 查看
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的数字
循环:用两个数组分别保存i-1的情况和i的情况 i from 1 to n
递归:参数记录剩余需要生成的长度和最小的能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 = ['1'] (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
相关文章推荐
- swift get,set
- swift protocol
- #pragma mark
- c++中调用cygwin/x使用ncl
- 我的MySQL使用手记
- 使用JDBC连接MySQL数据库--典型案例分析(二)--封装数据库的连接与关闭
- 【ActionScript】ENTER_FRAME事件与组件的旋转
- IOS开发系列文章
- 搭建VTK+ITK+Python系统
- swift class
- mysql group by 使用技巧及其with rollup使用
- Linux下监听或绑定(bind)843端口失败
- 从几个sample来学习JAVA堆、方法区、JAVA栈和本地方法栈
- linux常用命令
- 欢迎使用CSDN-markdown编辑器
- java笔记10 多态 Object类
- POJ 1017 Packets
- Windows下使用NCL(Cygwin模拟Linux环境)
- (转) error: linker command failed with exit code 1 (use -v to see invocation)
- 解析mediaTypes+viewResolvers+viewResolvers+ContentNegotiatingViewResolver