ピタゴラスの定理a²+b²=c²が嫌いな人はいますか?値aとbを取り込んで、「この直角三角形の斜辺は」+ cと出力する任意の言語で可能な最短の方法を記述します。cは小数点以下3桁までにしてください。
code-golfタグは、「Code-golfはソースコードの最小バイトで特定の問題を解決するための競争相手です」と明示しています。得点コードゴルフ(バイト対文字)を参照してください。
ピタゴラスの定理a²+b²=c²が嫌いな人はいますか?値aとbを取り込んで、「この直角三角形の斜辺は」+ cと出力する任意の言語で可能な最短の方法を記述します。cは小数点以下3桁までにしてください。
code-golfタグは、「Code-golfはソースコードの最小バイトで特定の問題を解決するための競争相手です」と明示しています。得点コードゴルフ(バイト対文字)を参照してください。
回答:
'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:入力の値を2乗します+/:合計を取る.5*⍨:結果を0.5乗します3⍕:小数点以下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一種の陽気です。それを許可する場合、斜辺の長さが1バイトで指定されるようPolarGCにInput、beforeの設定も許可する必要がありますRか?確かに、とPolarGCの値XとYもはやされて表示されていない私たちは周りにカーソルを移動すると、彼らはまだしている保存された適切な変数に。(その後、使用することはありませんが、それは重要です。)
글坼各갠方終加감半方갾밈乘增貶껠矽녆둥긆둹댆뭴뉖멵댶넠닶눠덆둩댲걲늖덨덂건댦땡닦덬뉒걩댲밀⓶
入力は、スペースで区切られた2つの数値(小数でも構いません!)として期待されます。
これは、いくつかの不便なトリックを使用する必要があるにもかかわらず、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入力をstdinから読み取る必要がある場合:
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代わりに使用する場合、いくつかの文字を保存できます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?私は確かに、私は1000で、床の目的と+.5と乗算や除算を理解していないにもよ
{printf "The hypotenuse of this right triangle is %.3f
",sqrt [+] @_ X**2}
{}ラムダ関数を宣言します。[+]は合計演算子、X**クロスパワー演算子です(たとえば、1, 2 X+ 10, 20ギブス11, 21, 12, 22)。この場合、クロスベキ演算子は引数を1つ取るため、結果の長さはと同じになります@_。@_すべての関数引数が含まれます。
(危険な)引数の数が間違っかかる場合があります機能を持つことが許可されていないなら、それは交換することが可能だ[+] @_ X**2と$^a**2+$^b**2、どこ$^aと$^bプレースホルダ引数です。
途方もなく非効率的なアルゴリズム。
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する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)};ユーザーからインタラクティブに2つの入力を取得し、$ 1と$ 2に保存します。代わりに引数またはパイプライン入力を使用して、長さをある程度短縮できる場合があります。
"The hypotenuse of this right triangle is チャレンジ仕様ごとの出力に必要なテキスト。
$(... )"カプセル化されたコードブロックは、出力に含まれる前にスクリプトとして処理されます。
"{0:N3}"-f コードの次のビットからの出力を、小数点以下3桁の数字としてフォーマットします。
[math]::sqrt(... )カプセル化された値の平方根を取得します。
$1/1*$1+$2/1*$2「a ^ 2 + b ^ 2」として機能します。PowerShellで数値を自乗するのが最も簡単な方法ですが、整数に強制するには変数を1で割る必要があります。それ以外の場合、テキストとして扱われ、3 * 3 + 4 * 4は25ではなく3334444になります。
i=prompt,'The hypotenuse of this right triangle is '+Math.hypot(i(),i()).toFixed(3)
現在、stdin:D を使用した最短のJS実装
はFirefox 27.0+(EcmaScript 6)でのみ動作します
2つの変数のみを使用できる場合(多くのスクリプトがここで行うように):
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のソリューションとは異なり、私のものは関数を介して2つの変数を受け取り、結果のアラートを送信します。
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しても、バイトは節約されません。
0inputa,b:?"The hypotenuse of this right triangle is";sQ(a*a+b*b)
スクリーンショット:
cat("The hypotenuse of this right triangle is",round(sqrt(sum(scan()^2)),3))
cat STDOUTにコンテンツを表示します。
このscan()関数は、キーボードからユーザーの入力を受け取ります。この入力はベクトルとして存在し、その上に^2が適用され(^関数はベクトル化されます)、ベクトルsum()の要素を合計します。sqrt平方根を出力します。これは、小数点以下3桁に丸められます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であれば、出力は正常に見えます。