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. matlab 画三维图函数

    matlab三维绘图 http://blog.sina.com.cn/s/blog_6d5ffd0d0100lyah.html Matlab绘图系列之高级绘图 http://blog.163.com/ ...

  2. asp.net 页面 输出之前修改 html(render)

    protected override void Render(HtmlTextWriter writer) { StringWriter output = new StringWriter(); ba ...

  3. linux系统中rsync+inotify实现服务器之间文件实时同步

    最近需要对服务器上的文件实施动态备份,我又不想每次都手动来进行备份,在网上找了挺多资料,发现使用rsync就可以实现,如果想要实现实时同步,还可以使用rsync+inotify组合,本文就是以组合方式 ...

  4. KB975517 "The update does not apply to your system"

    https://www.manageengine.com/products//desktop-central/patch-management/Windows-Vista-Ultimate-Editi ...

  5. System.Data.SqlClient.SqlException.Number的所有错误值列表

    在系统数据库(master或msdb或model)的架构(sys)的视图(messages)中: SELECT [message_id]      ,[language_id]      ,[seve ...

  6. 【转】C#中判断扫描枪输入与键盘输入

    提出问题:在收货系统中,常常要用到扫描枪扫描条码输入到TextBox,当条码无法扫描时,需要手工输入.如果是扫描枪输入时,我们将自动去判读条码,而手工输入时,最终需要加按回车键确认后判读条码.这时候我 ...

  7. ios8 ios7 tableview cell 分割线左对齐

    ios8中左对齐代码 //加入如下代码 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cel ...

  8. J2EE学习中一些值得研究的开源项(转)

    这篇文章写在我研究J2SE.J2EE近三年后.前3年我研究了J2SE的Swing.Applet.Net.RMI.Collections. IO.JNI……研究了J2EE的JDBC.Sevlet.JSP ...

  9. Android Gradle实用技巧——多渠道打包

    友盟有很多不错的功能,例如渠道统计等. 想要做渠道统计,有一个要求就是要在manifest文件中添加各个渠道的配置.只有一两个渠道还好说,但是渠道多了的话,手动修改然后打包简直是噩梦. 幸好现在And ...

  10. MVC 安装

    mvc 4 支持window xp,window 7,window 8, mvc 4 支持vs2010,vs2012 vs2012中包含mvc4; vs2010中需要安装mvc4 安装包: