ログインしてください。
公式
B - Traveling Takahashi Problem 解説
by
B - Traveling Takahashi Problem 解説
by
kyopro_friends
問題文の指示通りに実装しましょう。最後に原点に戻る必要があることに注意してください。
多くの言語では sqrt
という名前の関数を用いて平方根の計算を行うことができます。
\(\sqrt{x^2+y^2}\) を計算することができる hypot(x,y)
という関数を持つ言語もあります。
言語によっては、答えを出力する際の小数点以下の桁数にも注意が必要です。
実装例(Python)
import math
N = int(input())
points = [tuple(map(int,input().split())) for _ in range(N)]
points = [(0,0)] + points + [(0,0)]
ans = 0
for i in range(N+1):
px, py = points[i]
qx, qy = points[i+1]
ans += math.hypot(px-qx, py-qy)
print(ans)
実装例(C)
#include<stdio.h>
#include<math.h>
int main(){
int n;
scanf("%d", &n);
double ans = 0;
int crrx = 0;
int crry = 0;
for(int i=0; i<n; i++){
int x, y;
scanf("%d%d", &x, &y);
ans += hypot(x-crrx, y-crry);
crrx = x;
crry = y;
}
ans += hypot(crrx, crry);
printf("%.10f\n", ans);
}
投稿日時:
最終更新: