排序串联的序列


17

考虑一个基于递归关系的序列f(n) = f(n-1)+f(n-2),以开头f(1) = x1, f(2) = x2。对于x1 = 2, x2 = 1,序列开始如下:

2  1  3  4  7  11  18  29  47  76  123  199  322  521  843

将其串联成字符串将得到:

213471118294776123199322521843

现在,将该列表划分为的最小可能数y(n) > y(n-1)。从第一个数字开始,然后从第二个数字开始,等等。第一个输出数字应始终为一位数字。用所需的零数字填充最后一个数字。

2 13 47 111 829 4776 12319 93225 218430

您将以(x1, x2)任何方便的格式得到两个数字作为输入,而挑战是输出排序后的列表。

规则:

  • 功能和程序还可以
  • 初始序列应恰好有15个数字(最后一个数字是f(15))。
  • x1并且x2为非负数(可能为零)。
  • 输出可以是任何方便的格式
  • y必须创建输出矢量,以便y2 > y1
    • 首先是最小的可能y1,然后最小的可能y2,然后y3依此类推。
  • 如果是,x1 = x2 = 0则输出15个零(与其他输出格式相同,即不是000000000000000)。

例子

Input: 1 1
Output: 1  12  35  81  321  345  589  1442  3337 7610

Input: 3 2
Output: 3  25  71  219  315  0811 3121  23435 55898 145300
                             |
                             Optional leading zero 
Input: 0 0
Output: 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

以字节为单位的最短代码获胜。如果可能,请提供指向在线口译员的链接。


“最小数字”到底是什么意思?最小的平均值?最小最大?还有别的吗
isaacg

@isaacg所以第n个数字大于第(n-1)个。
尼卡

1
为了澄清我的问题,正确的分割将5467是什么?54 675 46 70
isaacg


3
0似乎是一个令人讨厌且不必要的异常。
Martin Ender'1

Answers:


1

Pyth,56个字节

LsgM.:sMb2?sQsM.WyHX_1Z`0u?yGX_1GHaGHjkhM.u,eNsN14QYmZ15

测试套件

说明:

首先,我们检查输入是否准确 0, 0。如果是这样,请打印15个零。

否则,我们产生序列, jkhM.u,eNsN14Q。这类似于斐波那契序列的标准Pyth算法。

接下来,我们减少该字符串。累加器是一个字符串列表,表示划分后的序列中的每个数字。在每个归约步骤中,我们采用下一个字符,并使用y定义为LsgM.:sMb2如果输入顺序不正确,则为真。如果顺序正确,我们会将下一个字符作为自己的编号附加到列表中。如果没有,我们将下一个字符添加到最后一个字符串的末尾。这是通过完成u?yGX_1GHaGH ... Y

接下来,我们执行功能性的while循环。循环继续进行,直到运行列表正确为止,重新使用了辅助函数。在每一步,0添加到列表中最后一个字符串的末尾。这是通过完成的.WyHX_1Z`0

最后,使用,将字符串转换为整数sM并进行打印。


Pyth,51个字节

LsgM.:sMb2?sQsM.WyHX_1Z`0hf!yT_./jkhM.u,eNsN14QmZ15

我相信这可行,但是测试起来太慢了-这是分割字符串的蛮力解决方案。


我将对该X函数进行一些改进,但是上面的代码在发布问题时最新的Pyth版本中有效。


5

JavaScript的ES6,127 135

(a,b)=>eval("for(n=r=[],v=13,o=a+n+b;v--;a=b,b=t)o+=t=b+a;for(d of o+'0'.repeat(99))(n+=d)>+v&&(r.push(v=n),n='');+v?r:[...o]")

测试

F=(a,b)=>eval("for(n=r=[],v=13,o=a+n+b;v--;a=b,b=t)o+=t=b+a;for(d of o+'0'.repeat(99))(n+=d)>+v&&(r.push(v=n),n='');+v?r:[...o]")

// less golfed

U=(a,b)=>{
  for(n=r=[], o=a+n+b, v=13; v--; a=b, b=t)
    o+= t= b+a;
  for(d of o+'0'.repeat(99))
    if ((n+=d) > +v)
      r.push(v=n), n='';
  return +v ? r : [...o]
}

function test(){
  var i = I.value.match(/\d+/g)
  O.textContent = i.length > 1 ? F(+i[0],+i[1]) : ''
}
test()
A,B : <input id=I value='0 1' oninput='test()'>
<pre id=O></pre>


x1 = 0,x2> 0时发生错误,例如输入“ 0 1”。
flornquake

@flornquake已修复。字节数保持不变,减少了零填充代码
edc65

2

的JavaScript ES6,187 180 187 184 182 179 175 172 165个 160 155 154字节

(a,b)=>eval('d=""+a+b;for(i=-12,j=1;++i<99;)i<2?(c=b,d+=b=a+b,a=c,r=a?[d[0]]:"0,".repeat(15)):(f=+d.slice(j,i))>r[r.length-1]?(r.push(f),j=++i-1):d+=0;r')

我得到了类似的结果时运行1,13,2测试案例。0,0已经占用了多余的26个字节...

去高尔夫+转换为ES5 +演示:

function s(a, b) {
  d = "" + a + b;
  for (i = -12, j = 1; ++i < 99;)
    i < 2 ?
      (c = b, d += b = a + b, a = c, r = a ? [d[0]] : "0,".repeat(15))
    : (f = +d.slice(j, i)) > r[r.length - 1] ?
      (r.push(f), j = ++i - 1)
      : d += 0;
  return r
}
document.write(
   s(1,1)+"<br>"+
   s(3,2)+"<br>"+
   s(0,0)
)


为什么会产生更多的数字?而且难道不容易修复吗?要求是n <= 15
Stewie Griffin

@Stewie但是,嘿,第一个产生12,第二个产生11。小于
15。– nicael

初始序列f(n) = f(n-1)+f(n-2)的最大值恰好为15。输出值的数量是根据算法确定的,仅此而已。
Stewie Griffin

@Stewie好的,所以它必须恰好是15,对吧?那么,n <= 15表示输入数字小于15?
nicael

初始序列中的值数为15。起始值 f(1)=x1f(2)=x2可以比更高15.输出值的数目是基于所述输入值来确定的。对于3 2这将是10
Stewie新

1

JavaScript(ES6),162个字节

(a,b)=>(k=[...Array(15).keys(y="")],p=-1,z=k.map(_=>0),a|b?[...k.map(f=n=>n--?n?f(n)+f(n-1):b:a).join``,...z].map(d=>+(y+=d)>p?(p=y,y=" ",p):"").join``:z.join` `)

说明

(a,b)=>(
  k=[...Array(15).keys(y="")],     // k = array of numbers 0 to 14, initialise y
  p=-1,                            // initialise p to -1 so that 0 is greater than p
  z=k.map(_=>0),                   // z = array of 15 zeroes
  a|b?[                            // if a and b are not 0
      ...k.map                     // for range 0 to 14
      (f=n=>n--?n?f(n)+f(n-1):b:a) // recursive sequence function (0 indexed)
      .join``,                     // join result of f(0) to f(14) as a string
      ...z                         // append zeroes for padding
    ].map(d=>                      // for each digit of concatenated result
      +(y+=d)                      // append the digit to the current number y
      >p?(                         // if the current number is greater than the previous p
        p=y,                       // set previous to the current number
        y=" ",                     // reset y (with space as a separator)
        p                          // output the current number (with space at the start)
      ):""                         // else add nothing to the output
    )
    .join``                        // return the output as a string
  :z.join` `                       // return a bunch of zeroes if a and b are 0
)

测试


1

Mathematica,192个字节

f[{0,0}]:=0~Table~15
f@l_:=(t=0;q={};If[#>0,q~Join~{10^⌈Log10[t/#]⌉#},q]&[Last@#]&@FoldList[If[#>t,AppendTo[q,t=#];0,#]&[10#+#2]&,0,Flatten@IntegerDigits@SequenceFoldList[#+#2&,l,Range@13]])

测试用例:

f[{2, 1}]
(* {2, 13, 47, 111, 829, 4776, 12319, 93225, 218430} *)
f[{3, 2}]
(* {3, 25, 71, 219, 315, 811, 3121, 23435, 55898, 145300} *)
f[{0, 0}]
(* {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} *)

函数名称的长度使我丧命。


1

Haskell中,165 159 152 142 141字节

w=take 15
x#y=x:scanl(+)y(x#y)
0%0=w[0,0..]
x%y=g(-1)(w(x#y)++0%0>>=show)(-1)
g _""_=[]
g b l@(h:t)a|b>a=b:g 0l b|1<2=g(max 0b*10+read[h])t a

用法示例: 3 % 2 -> [3,25,71,219,315,811,3121,23435,55898,145300]

在线演示(带有main包装)。

怎么运行的:

w=take 15
x#y=x:scanl(+)y(x#y)              -- fibonacci sequence generator for x and y

0%0=w[0,0..]                      -- special case 0%0
x%y=g(-1)(w(x#y)++0%0>>=show)(-1) -- calculate fib sequence, add some extra 0 and
                                  -- flatten all digits into a single string.
                                  -- start calculating the resulting sequence

g _""_=[]                         -- if we don't have digits left, stop.
                                  -- the final 0 in the second parameter is ignored.
g b l@(h:t)a
  |b>a=b:g 0l b                   -- if the current number is greater than the
                                  -- previous one, take it and start over.
  |1<2=g(max 0b*10+read[h])t a    -- otherwise add the next digit and retry.
                                  -- The "max" fixes the initial call with -1.

0

PowerShell,167166字节

param($x,$w)if($w-lt($x-eq0)){"0`n"*15;exit}[char[]]("$x"+-join(0..13|%{$w;$w=$x+($x=$w)}))|%{$z+="$_";if(+$z-gt$y){($y=$z);$z=""}};if($z){while(+$z-lt$y){$z+="0"}$z}

通过消除 $s变量并直接馈入输出循环来。

取消评论并评论:

param($x,$w)           # Take input parameters as x and w
if($w-lt($x-eq0)){     # If x=0, ($x-eq0)=1, so $w-lt1 implies w=0 as well
  "0`n"*15             # Print out 15 0's separated by newlines
  exit                 # And exit program
}                      # otherwise ...
[char[]](              # Construct the sequence string as a char-array
"$x"+-join(            # Starting with x and concatenated with a joined array
  0..13|%{             # Loop
    $w                 # Add on w
    $w=$x+($x=$w)      # Recalculate for next loop iteration
  }
))|%{                  # Feed our sequence as a char-array into a loop
  $z+="$_"             # z is our output number, starts with the first digit
  if(+$z-gt$y){        # If z is bigger than y (initialized to 0)
    ($y=$z)            # Set y equal to z and print it
    $z=""              # Reset z to nothing to start building the next number
  }
}
if($z){                # If there is remaining digits, we need to pad zeroes
  while(+$z-lt$y){     # Until z is bigger than y
    $z+="0"            # Tack on a zero
  }
  $z                   # Print the final number
}

0

Perl 6,107个字节

{$_=@=(|@_,*+*...*)[^15].join.comb;.sum??[.shift,{last if !@$_;until (my$a~=.shift//0)>$^b {};$a}...*]!!$_} # 107

用法:

# give it a lexical name for ease of use
my &code = {...}

# use 「eager」 because the anonymous block returns a lazy array
# and 「say」 doesn't ask it to generate the values
say eager code 2, 1;
# [2 13 47 111 829 4776 12319 93225 218430]
say eager code 1, 1;
# [1 12 35 81 321 345 589 1442 3337 7610]
say eager code 3, 2;
# [3 25 71 219 315 0811 3121 23435 55898 145300]
say eager code 0, 0;
# [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
say eager code 0, 1;
# [0 1 12 35 81 321 345 589 1442 3337 7000]

说明

创建类似Fibonacci的序列,从参数(@_)slipped(|)开始

|@_,*+*...*

接受该序列的前15个元素

(…)[^15]

将其合并为单个字符串(.join),将其拆分为单个字符序列(.comb),并在$_将序列强制转换为可变数组后,首先将其存储在匿名数组(@)中,然后将其存储在“默认”标量()中

$_=@=(…)[^15].join.comb;

它会在默认标量中找到值的总和,如果该值为零,则返回默认标量,该标量将包含15个零的数组

.sum??  !!$_

如果总和不为零,则通过首先移出默认标量中的第一个元素来创建列表

.shift,  

随后生成的值的其余部分,检查它针对上一个($^b
如果默认运行标量的值时,使用0代替(//0

…,{  ;until (my$a~=.shift//0)>$^b {};$a}...*

当默认标量中没有剩余元素时停止

…,{last if !@$_;  }...*

为什么要在其中留出空间until (my$a...?是(不是一个特殊的分隔符?

@cat将是对名为的子例程的调用,该子例程until不存在。
布拉德·吉尔伯特b2gills '16
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.