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 = [‘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
时间: 2024-07-29 07:42:11

Epic - Desirable Number的相关文章

Epic - Decimal Number

Let the user enter a decimal number. Therange allowed is 0.0001 to 0.9999. Only four decimal places are allowed. Theoutput should be an irreducible fraction. E.g.: If the user enters 0.35,the irreducible fraction will be 7/20. 等于找与10000的公约数 def fract

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 seed for a given number. 辗转相除法,由性质可利用 sqrt(num) <= seed_number <= num/2 缩小范围. def seed_number(num) seed = Math.sqrt(num).to_i while seed <= num /

Cipe Coding Summary Part1

1. Colorful Number:A numbercan be broken into different sub-sequence parts. Suppose a number 3245 can bebroken into parts like 3 2 4 5 32 24 45 324 245. And this number is a colorfulnumber, since product of every digit of a sub-sequence are different

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/problem/E Description Aladdin had found a new shiny lamp and has started polishing it with his hands. Suddenly a mysterious genie appeared from withi

codeforces Epic Game 题解

Simon and Antisimon play a game. Initially each player receives one fixed positive integer that doesn't change throughout the game. Simon receives number a and Antisimon receives number b. They also have a heap of nstones. The players take turns to m

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

17. Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

实现一个函数clone,使JavaScript中的5种主要的数据类型(包括Number、String、Object、Array、Boolean)进行值复制

实现一个函数clone,可以对JavaScript中的5种主要的数据类型(包括Number.String.Object.Array.Boolean)进行值复制. 1 /** 对象克隆 2 * 支持基本数据类型及对象 3 * 递归方法 */ 4 function clone(obj) { 5 var o; 6 switch (typeof obj) { 7 case "undefined": 8 break; 9 case "string": o = obj + &q

解决sqoop报错Invalid number; item = ITEM_UNICODE

报错栈: java.sql.SQLException: Invalid number; item = ITEM_UNICODE at com.intersys.jdbc.SysList.getInt(SysList.java:1735) at com.intersys.jdbc.CacheResultSet.getInt(CacheResultSet.java:247) at org.apache.sqoop.lib.JdbcWritableBridge.readInteger(JdbcWrit