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

Epic - Desirable Number的更多相关文章

  1. Epic - Seed Number

    Find the seed of a number. Eg : 1716 = 143*1*4*3 =1716 so 143 is the seed of 1716. find all possible ...

  2. Epic - Decimal Number

    Let the user enter a decimal number. Therange allowed is 0.0001 to 0.9999. Only four decimal places ...

  3. ACM: 限时训练题解-Epic Professor-水题

    Epic Professor   Dr. Bahosain works as a professor of Computer Science at HU (Hadramout    Universit ...

  4. CF Gym 100685E Epic Fail of a Genie

    传送门 E. Epic Fail of a Genie time limit per test 0.5 seconds memory limit per test 64 megabytes input ...

  5. Codeforces gym 100685 E. Epic Fail of a Genie 贪心

    E. Epic Fail of a GenieTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685 ...

  6. MapReduce: number of mappers/reducers

    14 down vote It's the other way round. Number of mappers is decided based on the number of splits. I ...

  7. How to create functions that can accept variable number of parameters such as Format

    http://www.chami.com/tips/delphi/112696D.html Sometimes it's necessary to pass undefined number of [ ...

  8. JavaScript Math和Number对象

    目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...

  9. Harmonic Number(调和级数+欧拉常数)

    题意:求f(n)=1/1+1/2+1/3+1/4-1/n   (1 ≤ n ≤ 108).,精确到10-8    (原题在文末) 知识点:      调和级数(即f(n))至今没有一个完全正确的公式, ...

随机推荐

  1. 【转】8张图理解Java

    一图胜千言,下面图解均来自Program Creek 网站的Java教程,目前它们拥有最多的票选.如果图解没有阐明问题,那么你可以借助它的标题来一窥究竟. 1.字符串不变性 下面这张图展示了这段代码做 ...

  2. linux awk命令

    简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...

  3. ASP.NET MVC 基础(01)

    [ASP.NET MVC ]系列文章大致会包含这样一些内容: 1.ASP.NET MVC 的一些高级知识点: 2.ASP.NET MVC 的一些最新技术: 3.ASP.NET MVC 网站安全方面的知 ...

  4. IO - IOUtils

    Commons IO is a library of utilities to assist with developing IO functionality. There are four main ...

  5. PLSQL_性能优化系列10_Oracle Array数据组优化

    2014-09-25 Created By BaoXinjian

  6. CE_现金预测详解(案例)

    2014-07-14 Created By BaoXinjian

  7. Codeforces Round #365 (Div. 2) Chris and Road

    Chris and Road 题意: 给一个n个顶点的多边形的车,有速度v,人从0走到对面的w,人速度u,问人最快到w的时间是多少,车如果挡到人,人就不能走. 题解: 这题当时以为计算几何,所以就没做 ...

  8. POJ 2393 贪心 简单题

    有一家生产酸奶的公司,连续n周,每周需要出货numi的单位,已经知道每一周生产单位酸奶的价格ci,并且,酸奶可以提前生产,但是存储费用是一周一单位s费用,问最少的花费. 对于要出货的酸奶,要不这一周生 ...

  9. [实变函数]2.3 开集 (open set), 闭集 (closed set), 完备集 (complete set)

    1        $$\beex \bea E\mbox{ 是开集}&\lra E^o=E\\        &\lra \forall\ P_0\in E,\ \exists\ U( ...

  10. docker一些命令

    1.创建image(先创建Dockerfile) docker build -t xxx/xxx . 2.删除image docker rmi xxxxx(image id) docker rmi r ...