数组对齐加法


39

介绍

考虑两个非空整数数组,例如A = [0 3 2 2 8 4]B = [7 8 7 2]。要对其进行对齐添加,请执行以下操作:

  1. 重复每个数组足够的时间,使其总长度为lcm(length(A),length(B))。在这里,lcm代表最低公倍数。

    A -> [0 3 2 2  8 4][0 3  2 2 8 4]
    B -> [7 8 7 2][7 8  7 2][7 8 7 2]
    
  2. 在重复的数组上执行逐元素加法,并在每个数组中都有剪切的位置处剪切结果。

    A -> [0  3 2 2   8  4][0 3  2  2  8 4]
    B -> [7  8 7 2][ 7  8  7 2][7  8  7 2]
      -> [7 11 9 4][15 12][7 5][9 10 15 6]
    
  3. 此数组数组是您的结果。

任务

您的输入是两个非空的整数数组,并且您的输出将是它们的对齐加法的结果,如上面所定义。输入和输出可以采用任何合理的格式。执行加法时,您不必担心整数溢出。

规则和计分

您可以编写完整的程序或函数。最低字节数获胜。

测试用例

[1] [4] -> [[5]]
[1,2,-3,-4] [15] -> [[16],[17],[12],[11]]
[0,-4] [2,1,0,-3] -> [[2,-3],[0,-7]]
[0,3,2,2,8,4] [7,8,7,2] -> [[7,11,9,4],[15,12],[7,5],[9,10,15,6]]
[18,17,16] [-1,-2,-3,-4] -> [[17,15,13],[14],[16,14],[15,13],[15],[16,14,12]]
[18,17,16,15] [-1,-2,-3,-4] -> [[17,15,13,11]]
[1,1,1,1,1] [6,5,6,5,6,5,6,2,1] -> [[7,6,7,6,7],[6,7,3,2],[7],[6,7,6,7,6],[7,3,2],[7,6],[7,6,7,6,7],[3,2],[7,6,7],[6,7,6,7,3],[2],[7,6,7,6],[7,6,7,3,2]]
[1,1,1,1,1,1] [6,5,6,5,6,5,6,2,1] -> [[7,6,7,6,7,6],[7,3,2],[7,6,7],[6,7,6,7,3,2]]
[1,1,1,1,1,1,1] [6,5,6,5,6,5,6,2,1] -> [[7,6,7,6,7,6,7],[3,2],[7,6,7,6,7],[6,7,3,2],[7,6,7],[6,7,6,7,3,2],[7],[6,7,6,7,6,7,3],[2],[7,6,7,6,7,6],[7,3,2],[7,6,7,6],[7,6,7,3,2],[7,6],[7,6,7,6,7,3,2]]

C没有办法知道数组的长度-我可以请求数组的长度作为参数,还是将其存储在数组的开头?
cat

1
@cat如果没有其他获取长度的方法,可以将其作为额外的参数。
Zgarb

Answers:


9

JavaScript(ES6),101 99字节

将输入作为2个数组。返回一个字符串。

f=(a,b,j=0,s='')=>a.map((v,i)=>(s+=i*j?' ':s&&'][',s+=b[j]+v,j=++j%b.length))|j?f(a,b,j,s):`[${s}]`

这个怎么运作

我们a用一个指针迭代第一个数组,i同时将另一个指针更新j到第二个数组b。总和a[i] + b[j]被附加到输出字符串s。每次i == 0或都会插入一个分隔符j == 0。我们重复此过程,直到j恰好在b迭代结束时才返回。

注意:|被施加操作者,a.map(...)被强制转换到要么NaN(如果a包含一个以上的元素)或的当前值j(如果a只包含一个元素)。因此,a.map(...)|j == j在任何情况下都可以在此处安全使用。

测试用例


我什至没有试图理解答案,对于笔记 +1 。我将复制并保留在需要时粘贴
edc65 '16

6

Haskell,84 79字节

a#b=a%b where(c:d)%(e:f)|(x:y)<-d%f=(c+e:x):y;[]%[]=[[]];c%[]=[]:c%b;_%d=[]:a%d

我的第一个版本在更具可读性的布局中是相同的:

a#b=a%b where
 (c:d)%(e:f)|(x:y)<-d%f=(c+e:x):y
 []%[]=[[]]
 c%[]=[]:c%b
 _%d=[]:a%d

使用局部定义避免(%)a和提供额外的参数b。令人惊讶的是,这几乎与@nimi几乎在同一时间给出的解决方案相同,从中我想到了只对本地定义使用一行的想法。

用法:

*Main> [0,3,2,2,8,4] # [7,8,7,2]
[[7,11,9,4],[15,12],[7,5],[9,10,15,6]]

哦,这是将总和添加到列表的第一个元素的好方法。比我笨拙的要短得多!
nimi 2016年

4

PHP,126120字节

function($a,$b){do{$c[$j][]=$a[$i%$x=count($a)]+$b[$i%$y=count($b)];++$i%$x&&$i%$y?:$j++;}while($i%$x|$i%$y);return$c;};

在这里尝试!

返回结果数组数组的匿名函数。

本质上,我们遍历两个数组的内容,并根据数组的长度修改迭代器,以模拟“复制”它们。从数组中获取每个值,我们将它们求和并将其添加到中的数组中$c。如果我们到达输入数组之一的末尾(就挑战而言为分割),则开始在中分配给新数组$c

发生do while循环的原因是因为我们的条件基于$i,所以从开始0。如果我们使用在开始时检查条件的循环,则该循环将不会运行

我们只有在同时到达两个数组的末尾时才结束求和,这意味着LCM。


那不是$b[$i%$y]吗?通过移至$x=count($a)的第一个用法,您可以节省3个字节$x。相同$y=count($b),一个字节按位或处于while条件中
Titus

但是我认为匿名函数被视为摘要,因此不是有效的答案。
泰特斯(Titus)

根据对Meta的共识,默认情况下@Titus允许使用匿名函数。
Zgarb

感谢@Titus的建议,我只是把这个混为一谈,因为我想击败其他PHP答案:P
Xanderhall 2016年

4

Haskell,87 84字节

a#b=a%b where[]%[]=[[]];(e:f)%(g:h)=f%h!(e+g);e%[]=[]:e%b;_%g=[]:a%g
(m:n)!l=(l:m):n

用法示例:[0,3,2,2,8,4] # [7,8,7,2]-> [[7,11,9,4],[15,12],[7,5],[9,10,15,6]]

简单递归。基本情况:两个列表都是空的。如果其中只有一个为空,请使用完整版本重新启动,然后在输出中启动新集群。如果没有一个为空,则将和加在from元素之前。

还可以查看@Christian Sievers的答案,该答案几乎相同,并且是在几秒钟前发布的。


你确定吗?有没有办法得到确切的时间?
Christian Sievers,2016年

@ChristianSievers:我不知道您是否可以直接看到时间。当我们以分钟为单位显示编辑时间时,我记得您的编辑时间比我的要早几秒钟(大约20秒钟)。
nimi

您说对了:我在此页的html源代码中找到了时间戳
Christian Sievers,2016年

将鼠标悬停在“ 2天前答复”中的时间上,以查看确切时间。(提示:这是互联网上的标准用户界面,因此(a)如果您想要确切的时间,请尝试将相对时间悬停,以及(b)如果您实现了显示相对时间的内容,请在悬停时显示准确的时间!)
wchargin '16

2

八度,113字节

@(a,b)mat2cell(sum([repmat(a,1,(L=lcm(A=numel(a),B=numel(b)))/A);repmat(b,1,L/B)]),1,diff(unique([0:A:L,0:B:L])))

可直接调用此函数以将其放在括号中并作为(@(a,b)...)([1 2 3 4],[6 4 5])调用


1
现在,TIO-Nexus支持八度。这是测试代码
Luis Mendo,2016年

@LuisMendo谢谢,有趣的服务
rahnema1 '16

2

CJam,30个字节

{Sf*Laf+_s,f*:.+La/0=S2*a-Sa/}

在线尝试!

将输入作为一对列表。

说明

这个想法是在输入数组中插入一些标记(以短字符串的形式),这些标记指示对齐的数组在哪里结束,以及我们需要在数组中插入中断的位置。这样,我们可以避免必须计算LCM。

Sf*    e# Riffle each list with spaces. These are just place holders, so that having
       e# an array-end marker between two elements doesn't misalign subsequent elements.
Laf+   e# Append an empty string to each list. This is the array-end marker.
_s,    e# Convert the pair of lists to a string and get its length. This is always
       e# greater than the number of elements in either input.
f*     e# Repeat either array that many times. This is definitely more than necessary
       e# to reach the LCM (since multiplying by the length of the other list always
       e# gives a common multiple).
:.+    e# Pairwise addition of the list elements. There are four cases:
       e# - Both elements are numbers, add them. This is the actual addition
       e#   we need for the problem.
       e# - Both elements are spaces. This is just a regular position between
       e#   list elements.
       e# - One is a space, one is empty: the result is a single space, and
       e#   this marks a position where one of the arrays ended, which means
       e#   we need to split here.
       e# - Both elements are empty. This happens at the LCM of both list lengths
       e#   and indicates where we need to stop the output.
La/0=  e# Split the input around empty strings and discard everything except
       e# the first chunk.
S2*a-  e# Remove the double-space strings, we no longer need them.
Sa/    e# Split the list around single spaces.

2

果冻21 20 18字节

ṁ€L€æl/$S
J€ỊÇœṗÇḊ

在线尝试!

这个怎么运作

ṁ€L€æl/$S  Helper link. Argument [X, Y] (arrays of integers).

       $   Combine the two links to the left into a monadic chain.
  L€       Length each; yield the lengths of X and Y.
    æl/    Reduce by least common multiple.
ṁ€         Mold each; cyclically repeat the elements of X and Y to extend them
           to length lcm(length(X), length(Y)).
        S  Compute the sum of the extended X and Y arrays.

J€ỊÇœṗÇḊ   Main link. Argument [A, B] (arrays of integers).

J€         Indices each; replace A and B by the arrays of there 1-based indices.
  Ị        Insignificant; map 1 to itself, all other indices to 0.
   Ç       Apply the helper link to the result.
           This yield a Boolean array with a 1 (or 2) at all indices where a new
           repetition of A or B (or both) begins.
      Ç    Apply the helper link to [A, B].
    œṗ     Partition; break the result to the right at truthy elements (1 or 2) in
           the result to the right.
       Ḋ   Dequeue; remove the first element of the partition (empty array).

2

蟒3.5 - (146 137 134 130 + 12)= 142个字节

import math
def s(a,b):
 l,k,*r=map(len,[a,b])
 for i in range(l*k//math.gcd(l,k)):
  r+=a[i%l]+b[i%k],
  if i%k==k-1or i%l==l-1:print(r);r=[]

我无法弄清楚如何将整个for循环放在一行中。

编辑:


这给我带来了错误。该gcd函数在中fractions,不是math
Zgarb

@Zgarb 不建议使用gcd in fractions模块,您可以在此处检查更改。我猜rexter使用的是旧版本3.4.3
Gurupad Mamadapur '16

整洁,我不知道这一变化。不过,您应该将该语言标记为“ Python 3.5”,因为它在3.4或更早版本中不起作用。另外,您可以在圆括号中加上括号l*kprint(r);r=[]放在最后一行。
Zgarb

您确定字节数正确吗?我认为只有145个字节。
vaultah '16

1
我正在获取142个字节。您在使用Windows吗?Windows通常将换行符计为每个2字节,但此处每个换行符均计为一个字节。
mathmandan '16

2

Python 2,119字节

a=input()
i,v,l=0,list(a),len
while 1:q=l(v[0])>l(v[1]);print map(sum,zip(*v)[i:]);i=l(v[q]);v[q]+=a[q];1/(i-l(v[q^1]))

将来自stdin的输入作为两个用逗号分隔的元组,将结果列表输出到stdout。通过引发ZeroDivisionError异常来终止,因为这似乎是允许的

例如,如果输入为(0, 3, 2, 2, 8, 4), (7, 8, 7, 2),程序将打印

[7, 11, 9, 4]
[15, 12]
[7, 5]
[9, 10, 15, 6]

到stdout并将异常回溯到stderr。


您可以通过抛出错误来退出程序。这样一来,您就可以使循环成为一行。
Zgarb

2

J34 32字节

[:(<;.1~*)/[:+/*.&#$&>,:&(;2>#\)

在线尝试!

说明

[:(<;.1~*)/[:+/*.&#$&>,:&(;2>#\)  Input: array A (LHS), array B (RHS)
                             #\   Length of each prefix of A and B
                           2>     Less than 2
                          ;       Link each with A and B
                      ,:&         Pair them
                  #               Length of A and B
               *.&                LCM of the lengths
                    &>            For each box
                   $              Reshape it to the LCM of the lengths
           [:+/                   Reduce by addition
[:        /                       Reduce by
        *                           Sign of RHS
   <;.1~                            Box each partition of LHS

1

Haskell,166个字节

这可能不是最优雅的方法:基本上,该函数使用和?创建一个所需长度的列表,然后%再次将该和切掉。!是合并这两个的最终功能。

l=length
a?b=take(lcm(l a)$l b)$zipWith(+)(cycle a)$cycle b
l%(i:ind)|l==[]=[]|1>0=take i l:(drop i l)%(map(+(-i))ind)
a!b=(a?b)%[k|k<-[1..],k`mod`l a<1||k`mod`l b<1]

您可以用或替换indkdrop i l和周围有一些不必要的括号map(+(-i))ind。考虑也有两种情况%,其中模式匹配为l
Zgarb

1

[PHP],183个 152 135字节

function O($A,$B){while($f<2){$O[$k][]=$A[+$i]+$B[+$j];$f=0;isset($A[++$i])?:$i=!++$k|!++$f;isset($B[++$j])?:$j=!++$k|!++$f;}return$O;}

不错的版本:

function O($A,$B)
{
    while($f<2) {
        $O[$k][]=$A[+$i]+$B[+$j];
        $f=0;
        isset($A[++$i])?:$i=!++$k|!++$f;
        isset($B[++$j])?:$j=!++$k|!++$f;
    }

    return$O;
}

输出:

array (size=4)
  0 => 
    array (size=4)
      0 => int 7
      1 => int 11
      2 => int 9
      3 => int 4
  1 => 
    array (size=2)
      0 => int 15
      1 => int 12
  2 => 
    array (size=2)
      0 => int 7
      1 => int 5
  3 => 
    array (size=4)
      0 => int 9
      1 => int 10
      2 => int 15
      3 => int 6

使用这些调整与我一起绘制:$i=$j=$k=0;如果+$i在附加分配(-8个字节)中使用数组索引等,则不需要绘制。$i++;if(!isset($A[$i])){$i=0;$k++;}-> isset($A[++$i])?:$i=!++$k;(-9,两次)。$i==0&&$j==0&&!isset()-> !$i&!$j&!isset()(-6)。return$O;不需要空格(-1)。
泰特斯(Titus)

@Titus无法删除$i=$j=0;零件,因为数组中的第一个值不正确。我已经稍微修改了逻辑,所以不确定在这种情况下如何实现三元运算符。感谢您的++$i建议。
Dexa

尝试unset($i);$A[+$i]。该+会投null给整数0
泰特斯(Titus)2016年

if(!isset($A[++$i])){$i=0;++$k;++$f;}-> isset($A[++$i])?:$i=!++$k|!++$f;仍然每个保存5个字节。使用$f<2而不是保存一个$f!=2。和另外两个带有while($f=$f<3){...}代替while($f<2){$f=0;...}(初始化并重置$f为1,除非它增加了两次)
Titus

@Titus非常感谢,现在它更短了。
Dexa

1

PowerShell147145字节

param($a,$b)$o=@{};do{$o[+$j]+=,($a[+$i%($x=$a.count)]+$b[$i++%($y=$b.count)]);if(!($i%$x-and$i%$y)){$j++}}until(($x,$y|?{!($i%$_)}).count-eq2)$o

在线尝试!

欢迎打高尔夫球。我觉得可能还会再压缩10到15个字节。

将输入作为两个显式数组(具有@(...)语法)作为命令行参数。返回结果数组的哈希表,因为PowerShell中的多维数组会变得很奇怪,而且更加一致。设置一些初始变量,然后再次进入do/ until循环,条件是直到数组$ilcm count为止。

每次循环迭代时,我们将对应的$a$b值加在一起,将其视为数组,,(...)然后将其添加到哈希表$o的适当位置$j。数组封装对于防止算术加法是必需的- +=而是强制将重载改为数组串联。然后,使用条件on $x$y(计数)来确定我们是否在数组边缘上-如果是,则递增$j

最后,我们离开$o管道,输出是隐式的。
(注意:由于PowerShell如何使用默认值枚举哈希表Write-Output,因此倾向于向后输出;例如,结果数组“ 0th”位于输出的“底部”。哈希本身很好,并且可以如果您将这段代码封装在返回变量中,则使用该代码就很好了……在打印时,它看起来很奇怪。)

通过将$ x和$ y移入数组索引而不是分开(保存了两个分号),节省了2个字节。


1

Python 2,113字节

a,b=input()
i=m=n=0;r=[]
while(not i)+m+n:r+=[[]]*(not m*n);r[-1]+=[a[m]+b[n]];i+=1;m=i%len(a);n=i%len(b)
print r

请问notS为<1!而非?
Zgarb

1

蟒3.5,210 176 173 169 158字节

def f(a,b):
 x=[];e=f=0              
 while 1:
  if e==len(a):         
   print(x);x=[];e=0;
   if f==len(b):break
  if f==len(b):print(x);x=[];f=0
 x+=a[e]+b[f],;e+=1;f+=1

将两个列表作为输入并打印所有列表。

这是我的第一个答案,我还不知道如何打高尔夫球。我使用的基本思想是,每个列表都有两个计数器,用于指示拆分列表和当前列表,附加值将附加到该列表中;一旦遇到拆分,我们将打印当前列表并制作一个新的空列表。

  • 保存了34个字节:感谢DennisTimmyD
  • 保存了3个字节:在使用len(a)和len(b)的c和d时,发现它们没有用
  • 保存4个字节:感谢orlp,消除了不必要的寄生
  • 保存11个字节:重新排列一些块并将其压缩

1
嗨,欢迎来到编程难题和代码高尔夫球!不竞争意味着这里还有其他事情;您应该删除它。通过消除空格,您可以节省很多字节。例如,第2至5行可以变为x=[];c=len(a);d=len(b);e=f=0。同样,true可以成为1,并且x.append(a[e]+b[f])可以成为x+=a[e]+b[f],
丹尼斯

1
欢迎来到PPCG!除了Dennis的特定调整之外,请查看Python高尔夫技巧,以获取更多常规提示和技巧。
AdmBorkBork '16

1
ifwhile语句不需要括号。
orlp 2016年

1

球拍373字节

(let*((lg length)(fl flatten)(ml make-list)(t rest)(r reverse)(m modulo)(o cons)(ln(lg l))(jn(lg j))(c(lcm ln jn))(l2(fl(ml(/ c ln)l)))
(j2(fl(ml(/ c jn)j)))(ll(for/list((a l2)(b j2))(+ a b))))(let p((ll ll)(ol '())(tl '())(n 0))(cond[(empty? ll)(t(r(o(r tl)ol)))]
[(or(= 0(m n ln))(= 0(m n jn)))(p(t ll)(o(r tl)ol)(take ll 1)(+ 1 n))][(p(t ll)ol(o(first ll)tl)(+ 1 n))])))

取消高尔夫:

(define(f l j)
  (let* ((ln (length l))
         (jn (length j))
         (c (lcm ln jn))
         (l2 (flatten (make-list (/ c ln) l)))
         (j2 (flatten (make-list (/ c jn) j)))
         (ll (for/list ((a l2)(b j2))
               (+ a b))))

    ; TO CUT LIST INTO PARTS: 
    (let loop ((ll ll)
               (ol '())
               (templ '())
               (n 0))
      (cond
        [(empty? ll) 
         (rest (reverse (cons (reverse templ) ol)))]
        [(or (= 0 (modulo n ln))
             (= 0 (modulo n jn)))
         (loop (rest ll)
               (cons (reverse templ) ol)
               (list (first ll))
               (add1 n))]
        [(loop (rest ll)
               ol
               (cons (first ll) templ)
               (add1 n))]))))

测试:

(f '[1]  '[4])
(f '[1 2 -3 -4] '[15])
(f '[0 3 2 2 8 4]  '[7 8 7 2])

输出:

'((5))
'((16) (17) (12) (11))
'((7 11 9 4) (15 12) (7 5) (9 10 15 6))

1

Clojure,280206字节

(fn[a b](let[A(count a)B(count b)Q quot](map #(map last %)(partition-by first(take-while #((% 0)2)(map-indexed(fn[i s][[(Q i A)(Q i B)(or(= i 0)(>(mod i A)0)(>(mod i B)0))]s])(map +(cycle a)(cycle b))))))))

好吧,这更有意义。生成按元素求和,添加位置元数据,在我们还没有重复的时候获取,并将和值放入每个分区。

(def f (fn[a b]
         (let[A(count a)B(count b)Q quot]
           (->> (map +(cycle a)(cycle b))
                (map-indexed (fn [i s][[(Q i A)(Q i B)(or(= i 0)(>(mod i A)0)(>(mod i B)0))]s]))
                (take-while #((% 0)2))
                (partition-by first)
                (map #(map last %))))))

原文: 我希望对此进行改进,但这是我目前所拥有的最好的。

(fn[a b](let [C cycle o count c(take-while #(or(=(% 0)0)(>(% 1)0)(>(% 2)0))(map-indexed(fn[i[A B]][i(mod i(o a))(mod i(o b))(+ A B)])(map(fn[& v]v)(C a)(C b))))](map #(map last %)(partition-by first(map(fn[p c][p(last c)])(reductions + (map #(if(or(=(% 1)0)(=(% 2)0))1 0)c))c)))))

空洞而冗长:

(def f (fn[a b]
         (let [c(->> (map (fn[& v]v) (cycle a) (cycle b))
                     (map-indexed (fn[i[A B]][i (mod i(count a)) (mod i(count b)) (+ A B)]))
                     (take-while #(or(=(% 0)0)(>(% 1)0)(>(% 2)0))))]
           (->> (map (fn[p c][p(last c)]) (reductions +(map #(if(or(=(% 1)0)(=(% 2)0))1 0)c)) c)
                (partition-by first)
                (map #(map last %))))))

从“合并”一个无限循环的集合开始,a然后在集合中b每个元素的索引上添加元数据,直到两个序列都再次从索引0开始。

c然后,将此集合与分区数据(一和零的累加总和)合并,进行分区,然后选择最后一个元素(项的总和)。

我认为,要进行重大改进,就需要一种完全不同的方法。


1

PHP,150个 121 119字节

function($a,$b){while($i<2|$x|$y)$r[$k+=!($x=$i%count($a))|!$y=$i++%count($b)][]=$a[$x]+$b[$y];array_pop($r);return$r;}

匿名函数将输入作为数组。

分解

while($i<2|$x|$y)   // loop while either $a or $b has NO cut
    $r[
                // if either $a or $b has a cut, increment $k; post-increment $i
        $k+=!($x=$i%count($a))|!$y=$i++%count($b)
                // append current $a + current $b to $r[$k]
    ][]=$a[$x]+$b[$y];
array_pop($r);  // $r has one element too much; remove it
return$r;

0

C ++ 14,206字节

作为无名通用拉姆达,要求输入容器PQ和输出容器R要像vector<vector<int>>

[](auto P,auto Q,auto&R){R.clear();auto a=P.begin(),b=Q.begin(),x=P.end(),y=Q.end();auto A=a,B=b;do{R.emplace_back();while(a!=x&&b!=y)R.back().push_back(*a+++*b++);a=a==x?A:a;b=b==y?B:b;}while(a!=A||b!=B);}

脱胶和用法:

#include<vector>
#include<iostream>

using namespace std;

auto f=
[](auto P,auto Q,auto&R){
 R.clear();               //just clear the output to be sure
 //a and b are the iterators, x and y is the end
 auto a=P.begin(),b=Q.begin(),x=P.end(),y=Q.end();
 //just some abbreviations for .begin()
 auto A=a,B=b;
 do{
  R.emplace_back();      //add new vector
  while(a!=x&&b!=y)      //while not at the end of one vector
   R.back().push_back(*a+++*b++);  //add the pointed elements and advance
  a=a==x?A:a;            //reset if at the end   
  b=b==y?B:b;
 }while(a!=A||b!=B);     //if both were resetted, then finish
}
;


int main(){
 vector<int> A = {0, 3, 2, 2, 8, 4};
 vector<int> B = {7, 8, 7, 2};
 vector<vector<int>> R;
 f(A,B,R);
 for (auto c:R){
  for (int x:c)
   cout << x << ", ";
  cout << endl;
 }
 cout << endl;
}

0

Mathematica 112字节

这可能会得到改善。这个想法是创建一个2D数组,其中第二个元素用于跟踪计数器的出租人,并修改每个输入数组的长度。

Split[Table[{#[[1,(m=Mod[i,d=Length/@#,1])[[1]]]]+#[[2,m[[2]]]],Min@m},{i,LCM@@d}],#2[[2]]>#1[[2]]&][[;;,;;,1]]&

用法

%@{{0,3,2,2,8,4},{7,8,7,2}}

0

JavaScript(ES6),131个字节

(a,b,g=(r,[n,...d]=a,[m,...e]=b,s=[])=>1/n?1/m?g(r,d,e,[...s,n+m]):g([...r,s],[n,...d]):1/m?g([...r,s],a,[m,...e]):[...r,s])=>g([])

稍微松了一下:

(a,b,r=[],[n,...d]=a,[m,...e]=b,s=[])
=>1/n?1/m?f(a,b,r,d,e,[...s,n+m])
         :f(a,b,[...r,s],[n,...d],b,[])
     :1/m?f(a,b,[...r,s],a,[m,...e],[])
         :[...r,s]
  • 如果两个阵列de包含数字,所述第一数量的总和被附加到s与剩余的元件被递归处理
  • 如果其中一个数组包含数字,则将和数组s添加到结果中r,并将另一个数组重置为其初始数组
  • 如果两个数组都为空,则只需返回结果并附加最后的和。

可悲的是,此解决方案没有@Arnauld的无情效率,但至少我认为这是一个漂亮的解决方案。

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.