欧姆定律告诉我们,当施加电压(V)时,电流(I)通过欧姆中的电阻(R),如下所示:
V = I / R同样,由这种阻力消散的功率(P) (以瓦为单位)是由以下几个方面提供的:
P = V * I通过重排和替换,当给出其他两个量中的任何一个时,可以导出计算其中两个量的公式。这些公式概述如下(注意,此图像使用E而不是V表示伏特):

给定字符串中任意两个这些量的输入,则输出另外两个。
V A W R的电压,安培,电源和电阻(或等效小写)。此外,您可以使用Ω而不是R。单位将没有任何十进制前缀(基洛-,米利-等)。1W 1A
12V 120R
10A 10V
8R 1800W
230V 13A
1.1W 2.333V1V 1R
0.1A 1.2W
1R 100W
120V 15A
2990W 17.692R
0.471A 4.948R应该指出的是,解决这一挑战的办法实际上是自我逆转。换句话说,如果将解决方案应用于输入A B并获得输出C D,然后将解决方案应用于输入C D,则输出应该再次为A B,尽管可能是由于FP舍入而导致的无序和不安。因此,可以互换地使用测试输入和输出。
发布于 2016-03-09 10:17:30
输入作为函数参数。输出到带尾空间的标准输出(如有必要,可以修改)。
->s{a,b,c,d=s.split.map{|z|[z[-1],z.to_f]}.sort.flatten
%w{EA9.EAAVAA.WVA GS;.A?#WWV.RRR}.map{|w|m=w[n=(a+c+?!).sum%10].ord;print (b**(m%9-4)*d**(m/9-5))**0.5,w[n+7],' '}}所有公式都可以用b**x*d**y表示,其中b&d是两个输入值,x&y是幂。由于打高尔夫球的原因,(b**x*d**y)**0.5的表达式最终被选中,因为它意味着x和y在-4到4的范围内变成整数。
下表显示了所需的表达式(假设输入按字母顺序排序)和幂的编码值。其中x和y是倍幂,它们被编码为(x+4)+(y+4)*9+9或等效的(x+4)+(y+5)*9。这使得所有编码都在可打印的ASCII范围内。在简洁的公式中省略了电力运算符。
n是一种由输入单位符号生成的校验和,它可以取0,1,2,4,5,6 (不使用3)的值。
n formula 1 formula 2 formula 1 formula 2
value powers x+4 y+4 encoding powers x+4 y+4 encoding
0 A*R=V A2*R=W 1 1 6 6 69 E 2 1 8 6 71 G
1 R-1*V=A R-1*V2=W -1 1 2 6 65 A -1 2 2 8 83 S
2 R-.5*W.5=A R.5*W.5=V -.5 .5 3 5 57 9 .5 .5 5 5 59 ;
3 . . . .
4 A*V=W A-1*V=R 1 1 6 6 69 E -1 1 2 6 65 A
5 A-1*W=V A-2*W=R -1 1 2 6 65 A -2 1 0 6 63 ?
6 V-1*W=A V2*W-1=R -1 1 2 6 65 A 2 -1 8 2 35 #中未使用
f=->s{
a,b,c,d=s.split.map{|z|[z[-1],z.to_f]}. #split the input into an array [[symbol1,value1],[symbol2,value2]]
sort.flatten #sort alphabetically by symbol and flatten to assign the 4 objects to different variables
n=(a+c+?!).sum%10 #sum the ascii codes of the symbols (plus that of ! for good value distribution) and take mod 10. gives a number 0..6 (3 is not used)
%w{EA9.EAAVAA.WVA GS;.A?#WWV.RRR}. #for each of the outputs, there is a 14 character string. 1st 7 characters encode powers, 2nd 7 characters are output symbol
map{|w| #iterate through the 2 outputs
m=w[n].ord #select one character according to value of n and convert to a number encoding the powers to raise the two inputs to
print (b**(m%9-4)*d**(m/9-5))**0.5,w[n+7],' '#decode the powers, evaluate the expression and output, append the unit symbol and a space
}
}
f["6W 3A"]
puts
f["12V 120R"]
puts
f["10A 10V"]
puts
f["8R 1800W"]
puts
f["6W 2V"]
puts
f["2A 3R"]
puts2.0V 0.6666666666666666R
0.1A 1.2W
100.0W 1.0R
15.0A 120.0V
3.0A 0.6666666666666666R
6.0V 12.0Whttps://codegolf.stackexchange.com/questions/75152
复制相似问题