Epic - Desirable Number
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的更多相关文章
- 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 ...
- Epic - Decimal Number
Let the user enter a decimal number. Therange allowed is 0.0001 to 0.9999. Only four decimal places ...
- ACM: 限时训练题解-Epic Professor-水题
Epic Professor Dr. Bahosain works as a professor of Computer Science at HU (Hadramout Universit ...
- 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 ...
- 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 ...
- 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 ...
- 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 [ ...
- JavaScript Math和Number对象
目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...
- Harmonic Number(调和级数+欧拉常数)
题意:求f(n)=1/1+1/2+1/3+1/4-1/n (1 ≤ n ≤ 108).,精确到10-8 (原题在文末) 知识点: 调和级数(即f(n))至今没有一个完全正确的公式, ...
随机推荐
- 51nod1369 无穷印章
有一个印章,其完全由线段构成.这些线段的线足够细可以忽略其宽度,就像数学上对线的定义一样,它们没有面积.现在给你一张巨大的白纸(10亿x10亿大小的纸,虽然这个纸很大,但是它的面积毕竟还是有限的),你 ...
- php PDO连接数据库
[PDO是啥] PDO是PHP 5新加入的一个重大功能,因为在PHP 5以前的php4/php3都是一堆的数据库扩展来跟各个数据库的连接和处理,什么 php_mysql.dll.php_pgsql.d ...
- php获取一年中某一周的开始和结束时间
PHP来获取一年中的每星期的开始日期和结束日期的代码 函数get_week()通过传入参数$year年份,获取当年第一天和最后一天所在的周数,计算第一周的日期,通过循环获取每一周的第一天和最后一天的日 ...
- C语言每日一题之No.9
再做决定之前,我还是做好自己该做的.我不希望几年后会悔恨自己为什么在最该努力的时候不愿意吃苦.尊敬的女王陛下,请接题: 一.题目:有已按升序排好顺序的字符串a,编写程序将字符串s中的每个字符按升序的规 ...
- Linux下查看进程打开的文件句柄数和如何修改
修改文件句柄数在Linux下,我们使用ulimit -n 命令可以看到单个进程能够打开的最大文件句柄数量(socket连接也算在里面).系统默认值1024. 对于一般的应用来说(象Apache.系统进 ...
- [复变函数]第11堂课 3.3 Cauchy 积分定理及其推论
0. 引言 (1) Cauchy 积分定理: 设 $D$ 为 $(n+1)$ 连通区域, $f$ 在 $D$ 内解析且连续到边界 $C$, 则 $\dps{\int_C f(\zeta)\rd \ze ...
- Hibernae 的延迟加载
http://blog.csdn.net/xc635960736/article/details/7049863 Hibernae 的延迟加载 Hibernae 的延迟加载是一个非常常用的技术,实 ...
- css针对(各大浏览器、各版本)调兼容
ie6\ie7\firefox之下各自识别的CSS符号 #1 { color: #333; } /* firefox */ * html #1 { color: #666; } /* IE6 */ * ...
- protobuf使用说明
1..proto文件为要生成.java文件的模板文件,其中包含名称空间.文件名等信息2.cmd中进入当前目录D:\JAVA\protoc-2.5.0-win323.运行 protoc.exe --ja ...
- 30天轻松学习javaweb_模拟tomcat
运行 javac Server.java 编译java文件 执行 java Server 运行程序 在ie中输入 http://localhost:9999/ 打开模拟的服务程序 import jav ...