齿轮系,旋转


16

齿轮根据啮合齿轮的大小传递不同的速度量。

齿轮系

杰克有一台机器,它使齿轮系旋转。但您不知道最后一档的速度。

幸运的是,您是一位出色的高尔夫球手,所以您可以为他提供帮助!

所以我该怎么做?

每个齿轮由两个数字表示,内齿轮的半径和外齿轮的半径。

如果齿轮A[a,b]且齿轮B[c,d],则速度A与速度之比Bc:b

给定齿轮列表(2元组列表),输出最后一个齿轮的速度。

您可以假设第一档的速度为1

算出例子

假设我们的输入是[[6,12],[3,10],[5,8]]

一档[6,12]的速度为1

然后,第二个齿轮[3,10]的速度为1*12/3 = 4

然后,最后一个齿轮[5,8]的速度为4*10/5 = 8

测试用例

input                    output
[[1,1],[2,2]]            0.5     (1/2)
[[1,2],[1,2],[1,2]]      4       (2/1*2/1)
[[6,12],[3,10],[5,8]]    8       (12/3*10/5)

规则

基本的规则适用。


6
由于允许浮点输出,因此您可能应该澄清结果的精确度。
Martin Ender

我们可以将输入作为扁平化列表而不是元组列表吗?
Leaky Nun

是的,喜欢[6,12,3,10,5,8]。如果您想使用它,请提一下。

11
有点不公平。我有一个未发布的7字节版本,因为我认为它不太有趣。如果没有展平,它本来应该是6个字节。请考虑下次使用沙盒以避免这种情况。
丹尼斯

Answers:


8

Haskell,19个字节

foldr1(/).tail.init

给定一个平面列表一样[a,b,c,d,e,f]tail.init删除第一个和最后一个元素,然后foldr1(/)造成分歧的级联b/(c/(d/e))))该工程以交替*/b/c*d/e


但问题是程序会得到2元组的列表,而不是固定列表
显示名称

1
评论中允许使用清单。
xnor

7

果冻,6 个字节

ḊṖU÷@/

测试套件。

ḊṖU÷@/   Main monadic chain. temp <- third argument (first input)
Ḋ        temp <- temp with first element removed
 Ṗ       temp <- temp with last element removed
  U      temp <- temp reversed
   ÷@/   temp <- temp reduced by reversed floating-point division.
         implicitly output temp.

1
啊,交替师。那很聪明。
丹尼斯


5

C,115 123 121 83 80 76 71 70字节

多亏@LeakyNun,节省了4个字节!

我第一次打高尔夫球,可能不是最好的。

c;float r=1;float g(a,s)int*a;{for(;c<s-2;)r*=a[++c]/a[++c];return r;}

带有数组和大小。

取消高尔夫:

int counter;
float ret=1;
float gear(int *arr, int size) {
    for(; counter < size-2; )
        ret = ret * arr[++counter] / arr[++counter];
    return ret;
}

5
欢迎来到PPCG!:)
Martin Ender

您最多可以支持多少个号码?欢迎来到PPCG!
Leaky Nun

j;float r=1;float f(int a[]){for(;j<sizeof a;)r=r*a[j++]/a[j++];return r;}(未测试)
Leaky Nun

不是j ++,++ j和sizeof-2。保存4个字节。谢谢!
betseg '16

似乎我们无法测量传递的数组的大小。我编辑了答案。
betseg '16

4

JavaScript(ES6),44个字节

a=>(t=1,a.reduce((x,y)=>(t*=x[1]/y[0],y)),t)

展平数组的37个字节:

a=>1/a.slice(1,-1).reduce((x,y)=>y/x)

与(例如)Haskell不同的reduceRight是,它的名字太长了,以至于reduce用错误的方式便宜,并最终获得对等。


灵感答案有......我不能去了低于...
华利·威斯特


3

J,8个字节

%/@}:@}.

在线尝试!

用法

>> f =: %/@}:@}.

>> f 1 1 2 2
<< 0.5

>> f 1 2 1 2 1 2
<< 4

>> f 6 12 3 10 5 8
<< 8

>>STDIN 在哪里,<< STDOUT。

说明

J默认情况下,“减少” 从右到左,这占用了几个字节:p

divide       =: %
reduce       =: /
atop         =: @
remove_first =: }.
remove_last  =: }:

f =: (divide reduce) atop (remove_last) atop (remove_first)

3

Mathematica,26个字节

#2/#&~Fold~#[[-2;;2;;-1]]&

一个未命名的函数,该函数采用平坦的偶数长度值列表并返回精确结果(必要时作为分数)。

这使用与在反向列表上折叠除法的其他一些答案相同的方法(在删除第一个和最后一个元素之后)。


2

MATL,9字节

6L)9L&)/p

输入格式是以下任何一种:

[[6,12],[3,10],[5,8]]
[6,12,3,10,5,8]
[6 12 3 10 5 8]

编辑(2016年7月30日):链接的代码替换为9L1L以适应语言的最新更改。

在线尝试!

说明

6L    % Predefined literal: index from second to second-last element
)     % Apply index to implicit input. Removes first and last elements
9L    % Predefined literal: index for elements at odd positions
&)    % Two-output indexing. Gives an array with the odd-position elements
      % and the complementary array, with the even-position elements of the
      % original array
/     % Divide those two arrays element-wise
p     % Product of all entries. Implicitly display

1

JavaScript,54个字节

(a,s=1)=>a.map((v,i)=>s*=(x=a[i+1])?v[1]/x[0]:1).pop()

用法

f=(a,s=1)=>a.map((v,i)=>s*=(x=a[i+1])?v[1]/x[0]:1).pop()

document.write([
  f([[1,1],[2,2]]),
  f([[1,2],[1,2],[1,2]]),
  f([[6,12],[3,10],[5,8]])
].join('<br>'))

不打高尔夫球

function ( array ) {
  var s = 1;                                  // Set initial speed

  for ( var i = 0; i < array.length ; i++ ) { // Loop through array
    if ( array[i + 1] === undefined ) {       // If last element
      return s;                               // Return speed
    } else {                                  // Else
      s = s * ( array[i][0] / array[i+1][0])  // Calculate speed
    }
  }
}

当然,打高尔夫球的方式有些不同。使用.map(),它将数组的第一个值替换为第二个轮子之后的速度,第二个值替换为第三个轮子的速度,最后一个值和第二个最后一个值替换为最后一个轮子的速度。因此,我们只使用的最后一个元素.pop()


1

PHP,80 79 69字节

<?for($r=1;++$i<count($a=$_GET[a]);)$r*=$a[$i-1][1]/$a[$i][0];echo$r;

需要从GET参数输入a; 打印结果

$r用1 初始化,然后从第二个元组循环到最后一个元组,以与上一个元组的第一个元素相乘,然后除以当前元组的第二个元素。


感谢约尔格使我想起$_GET; 节省了7个字节。


更优雅的版本,88字节:

<?=array_reduce($a=$_GET[a],function($r,$x){return$r*$x[1]/$x[0];},$a[0][0]/end($a)[1]);

1
<?for($r=$i=1;$i<count($a=$_GET[a]);)$r*=$a[$i-1][1]/$a[$i++][0];echo$r;72字节
约尔格·赫尔斯曼

0

JavaScript,59 58 56字节

a=>a.reduce((p,c)=>p*c[1]/c[0],a[0][0]/a[a.length-1][1])

说明

缩小数组,然后乘以第二个值,再除以每个第一个值。因此,对于[[6,12],[3,10],[5,8]]12/6*10/3*8/5。当然,我们想要的实际计算是,12/3*10/5因此我们只想通过乘回并除以第一个/6和最后一个来忽略它。通过设置为reduce的初始值来完成抵消。*8*6/86/8


我来到了相同的解决方案。通过将后操作追加*.../...到初始值,可以节省两个字节1
泰特斯(Titus)


0

Python 3,59个字节

lambda x:eval('/'.join('{}*{}'.format(*i)for i in x)[2:-2])

一个匿名函数,该匿名函数通过参数接受非拼合列表的输入并返回输出。

怎么运行的

对于输入中的每对整数,'int1*int2'都会创建一个形式的字符串。将所有这些对连接在一起/将形成以下形式的字符串'int1*int2/int3*int4/...',这是所需的计算,但包括不需要的第一个和最后一个整数。通过在字符串中切出前两个字符和后两个字符来删除这些字符,从而保留所需的计算。然后对此进行评估并返回。

在Ideone上尝试


0

Pascal,88个字节

一个将静态2D数组及其长度(行数)作为输入的递归函数(必须这样做)。在数组上使用一些指针数学。

function r(a:p;n:integer):double;begin r:=a[1]/a[2];if n=2then exit;r:=r*r(a+2,n-1);end;

脱离用法示例:

type
  p = ^double;
var
  n: integer = 3;
  garray: array [0..2, 0..1] of double;

function ratio(a: p; n: integer): double;
begin
  ratio := a[1] / a[2];
  if n=2 then
    Exit;
  ratio := ratio * ratio(a+2, n-1);
end;

begin
  garray[0,0] := 6; garray[0,1] := 12;
  garray[1,0] := 3; garray[1,1] := 10;
  garray[2,0] := 5; garray[2,1] := 8;
  writeln(ratio(@garray, n));
end.

0

其实是14个位元组

pXdX2@╪k`i/`Mπ

在线尝试!(当前无法正常工作,因为TIO落后了几个版本)

该程序将一个扁平列表作为输入。

说明:

pXdX2@╪k`i/`Mπ
pXdX            remove the first and last elements
    2@╪k        push a list where each element is a list containing every two elements of the original list (chunk into length-2 lists)
        `i/`M   map division over each sublist
             π  product

0

R,64字节

事实证明,for在这种情况下,向量化方法和循环是等效的:

x=scan();prod(sapply(1:(sum(1|x)/2-1)*2,function(i)x[i]/x[i+1]))

for循环:

x=scan();for(i in 1:(sum(1|x)/2-1)*2)T=c(T,x[i]/x[i+1]);prod(T)}

`

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.