谁不喜欢勾股定理a²+b²=c²?用任何能用a和b取值并打印出“此直角三角形的斜边为+”的语言编写最短的方法。将c保留到小数点后三位。
code-golf
标签明确表示“代码高尔夫是在源代码最少字节中解决特定问题的竞赛。” 请参阅评分代码高尔夫球(字节与字符)。
谁不喜欢勾股定理a²+b²=c²?用任何能用a和b取值并打印出“此直角三角形的斜边为+”的语言编写最短的方法。将c保留到小数点后三位。
code-golf
标签明确表示“代码高尔夫是在源代码最少字节中解决特定问题的竞赛。” 请参阅评分代码高尔夫球(字节与字符)。
Answers:
'The hypotenuse of this right triangle is',3⍕.5*⍨+/⎕*2
测试:
'The hypotenuse of this right triangle is',3⍕.5*⍨+/⎕*2
⎕:
9 10
The hypotenuse of this right triangle is 13.454
说明:
⎕*2
:将输入中的值提高到第二个幂+/
:取总和.5*⍨
:将结果提高到0.5的幂3⍕
:舍入到小数点后三位Input :Disp "THE HYPOTENUSE OF THIS RIGHT TRIANGLE IS
Fix 3:R▶Pr(X,Y
否,不需要右括号。另外,比该APL答案少的字节:)
R▶Pr(A,B
。
Fix 3:R►Pr(X,Y
Input
索要X
并Y
是那种热闹。如果允许的话,是否还应该设置PolarGC
before Input
,以使斜边的长度由一个字节给出R
?可以肯定的是,当我们移动光标时,不再显示和PolarGC
的值,但是它们仍存储在适当的变量中。(那时我们将永远不会使用,但这才是最重要的想法。)X
Y
글坼各갠方終加감半方갾밈乘增貶껠矽녆둥긆둹댆뭴뉖멵댶넠닶눠덆둩댲걲늖덨덂건댦땡닦덬뉒걩댲밀⓶
期望输入为两个数字(可以是小数!),以空格分隔。
尽管必须使用一些不便的技巧,但它比APL短。
글坼 | split at space
各 | for each...
갠方 | to the power of two
終
加 | add
감半方 | to the power of one half
갾밈乘 | multiply by 1000
增貶 | increment, then decrement (kludge for rounding)
껠矽 | insert '.' at 4th-last character position
녆둥긆둹댆뭴뉖멵댶넠닶눠덆둩댲걲늖덨덂건댦땡닦덬뉒걩댲밀⓶ | "The hypotenuse..."
切线APL答案的分数!
2^r2^+3kv[The hypotenuse of this right triangle is ]Pp
测试:
$ dc
3 4
2^r2^+3kv[The hypotenuse of this right triangle is ]Pp
The hypotenuse of this right triangle is 5.000
dc -e '2^r2^+3kv[The hypotenuse of this right triangle is ]Pp'
不等待任何输入,打印"dc: stack empty"
3次,然后显示“此直角三角形的斜边为2.000”。
dc -e '3 4 2^r2^+3kv[...
参数3和4就是其中的地方。
如果输入只能是函数参数,则为77个字符:
f(a,b){printf("The hypotenuse of this right triangle is %.3f\n",hypot(a,b));}
99如果必须从标准输入中读取输入:
a,b;f(){scanf("%d %d",&a,&b);printf("The hypotenuse of this right triangle is %.3f\n",hypot(a,b));}
非常感谢@Yimin Rong!
电源外壳
只是看我是否可以...
echo "The hypotenuse of this right triangle is " ([math]::round([math]::sqrt(([math]::pow(([double](Read-Host -p "A")),2) + [math]::pow(([double](Read-Host -p "B")),2))),3))
Read-Host
。
p "The hypotenuse of this right triangle is %.3f"%(Math.sqrt(gets.to_i**2+gets.to_i**2))
更新(感谢您的评论):
p "The hypotenuse of this right triangle is %.3f"%(gets.to_i**2+gets.to_i**2)**0.5
a**0.5
而不是longy,则可以节省一些字符Math.sqrt(a)
。而且之后的空间p
也可以删除。
%(Math...)
。
print'The hypotenuse of this right triangle is %.3f'%(input()**2+input()**2)**.5
void h(int a,int b){printf("The hypotenuse of this right triangle is %.3f\n",hypot(a,b));}
pow(a,2)
什么时候可以做a*a
?我也不确定我是否了解底数和+.5以及乘以1000的目的
{printf "The hypotenuse of this right triangle is %.3f
",sqrt [+] @_ X**2}
{}
声明一个lambda函数。[+]
是求和运算符,X**
是交叉幂运算符(例如,1, 2 X+ 10, 20
给出11, 21, 12, 22
)。在这种情况下,交叉幂运算符采用一个参数,因此结果的长度与相同@_
。@_
包含所有函数参数。
如果它不允许有可能会采取错误的参数数目(不安全的)功能,它可以代替[+] @_ X**2
用$^a**2+$^b**2
,其中$^a
和$^b
是占位参数。
Java语言(97)
x=prompt;a=x(),b=x();x('The hypotenuse of this right triangle is '+Math.sqrt(a*a+b*b).toFixed(3))
一种荒谬的低效算法。
x;f(a,b){for(;x-a*a-b*b;x=rand());printf("The hypotenuse of this right triangle is %.3f",sqrt(x));}
program p;
{$APPTYPE CONSOLE}
var a,b:integer;
begin
readln(a,b);
writeln('the hypotenuse of this right triangle is',sqrt(b*b+a*a):2:3);
end.
integer
为来减少2个字符。int16
您不必在答案中包括前2行,也可以删除空格。完成所有操作后,您将获得106个字符。
Golfscript(69 67 66 65)
如果实际上支持浮点数而不使用解决方法,这将容易得多。
~'The hypotenuse of this right triangle is '@.*@.*+2-1??+.'.'?4+<
2.!~
时候2-1
比较短?
the difference between
和2-1
错误的信息,所以可能暂时感到困惑:)已修复,谢谢。
def p(a,b):print'The hypotenuse of this right triangle is %.3d'%((a*a+b*b)**.5)
math
一些节省。 (a*a+b*b)**.5
def
保存换行符和缩进符放在同一行上。
awk '{printf"The hypotenuse of this right triangle is %.3f\n",($1^2+$2^2)^.5}'
感谢Wasi建议^运算符并删除()!
例如
$ echo 3 4 | awk '{printf"The hypotenuse of this right triangle is %.3f\n",($1^2+$2^2)^.5}'
The hypotenuse of this right triangle is 5.000
{printf"The hypotenuse of this right triangle is %.3f\n",($1^2+$2^2)^.5}
高尔夫球码
1..2|%{sv $_ (read-host)};"The hypotenuse of this right triangle is $("{0:N3}"-f[math]::sqrt($1/1*$1+$2/1*$2))"
演练
1..2|%{sv $_ (read-host)};
从用户以交互方式获取两个输入,并将它们存储在$ 1和$ 2中。可以通过使用参数或管道输入来缩短长度。
"The hypotenuse of this right triangle is
根据挑战规范,输出中的必需文本。
$(
... )"
封装的代码块在包含到输出中之前将作为脚本处理。
"{0:N3}"-f
将代码的下一位输出格式设置为小数点后三位数字。
[math]::sqrt(
... )
获取封装值的平方根。
$1/1*$1+$2/1*$2
用作我们的“ a ^ 2 + b ^ 2”。在PowerShell中将数字本身相乘是最短的平方方法,但是必须先将变量除以1才能将其强制为整数。否则,它们将被视为文本,并且3 * 3 + 4 * 4将是3334444而不是25。
i=prompt,'The hypotenuse of this right triangle is '+Math.hypot(i(),i()).toFixed(3)
当前使用stdin
:D 的最短JS实现
仅在Firefox 27.0+(EcmaScript 6)上有效
如果我们只能使用两个变量(很多脚本在这里使用):
a=2,b=3,'The hypotenuse of this right triangle is '+Math.hypot(a,b).toFixed(3)
(也可以打印出No Such Method错误,尽管我不确定这是否违反规则)
class A{static{int a=1,b=1;System.out.printf("The hypotenuse of this right triangle is %.3f",Math.hypot(a,b));}}
(没有错误)
class A{static{int a=1,b=1;System.out.printf("The hypotenuse of this right triangle is %.3f",Math.hypot(a,b));}public static void main(String[] a){}}
C#
仅方法(114)
void H(double a, double b)
{
Console.Write("The hypotenuse of this right triangle is {0:N3}", Math.Sqrt(a * a + b * b));
}
完成课程(171)
using System;
class P
{
static void H(double a, double b)
{
Console.Write("The hypotenuse of this right triangle is {0:N3}", Math.Sqrt(a * a + b * b));
}
static void Main()
{
H(3, 4);
}
}
完整程序(不使用方法-141)
using System;class P{static void Main(){double a=3,b=4;Console.Write("The hypotenuse of this right triangle is {0:N3}",Math.Sqrt(a*a+b*b));}}
与@micha的解决方案不同,mine通过函数接受两个变量并发送结果警报。
function(a,b){m=Math;c=d=>d*d,e=1e3;alert("The hypotenuse of this right triangle is "+m.round(m.sqrt(c(a)+c(b))*e)/e)}
function(a,b){e=1e3;alert("The hypotenuse of this right triangle is "+Math.round(Math.sqrt(a*a+b*b)*e)/e)}
胖箭头功能可以解救!
h=(a,b,e=1e3)=>"The hypotenuse of this right triangle is "+Math.round(Math.sqrt(a*a+b*b)*e)/e
c()
。别名Math
不能节省字节数。
cat("The hypotenuse of this right triangle is",round(sqrt(sum(scan()^2)),3))
cat
将其内容显示到STDOUT。
该scan()
功能从键盘获取用户输入。此输入作为向量存在,在其^2
上应用(向量已向^
量化),并对sum()
向量的元素求和。sqrt
输出平方根,将其四舍五入到小数点后三位round(,3)
感谢@caird coinheringaahing注意到先前的答案没有舍入。
"The hypotenuse of this right triangle is "shnhn+A6`*N3eD
这只是输出字符串
"The hypotenuse of this right triangle is "s
hnhn+A6`*N3eD
hn take input and square it
hn take another input and square it
+ add them
A6` push 10^6
* multiply the sum with that number
N take integer square root
3eD output with three places of precision
implicit output
,²S½ær3µ,“¡ÆC⁷⁺ɱSoṿȤç½?⁶Ẏtḍỵŀ»ṚK
可能有更好的字符串压缩,它使我可以绕开需要与空格连接的地方,但是我找不到它。
说明:
,²S½ær3µ,“...»ṚK Example inputs: 3, 4
, Pair the inputs. Result: [3, 4]
² Square them. Result: [9, 16]
S Sum them. Result: 25
½ Get the square root of the sum. Result: 5
ær3 Round to 3 decimal places. Result: 5
µ Take the result of that... Result: 5
“...» ...and the compressed string Result: "The hypotenuse of this right triangle is"
, And put them into a pair. Result: [5, "The hypotenuse of this right triangle is"]
Ṛ Reverse that. Result: ["The hypotenuse of this right triangle is", 5]
k Join it with spaces. Result: "The hypotenuse of this right triangle is 5.0"
Implicit output.
only three decimal places
手段less than or equal to three decimal places
,输出看起来就很好。