p2365 2010/02/09

POJです。



自力で解いてないです。。。
ショートコーディング本に紹介されていた問題です。
参考

二点間の距離をもとめるのと、円周をもとめる公式がでてきますね。

実はRuntimeErrorがでて原因がわからずサブミットを何度もしてしまいました。
半径の入力がIntではなく、少数があるというわけでnextDoubleを使って解決。。。

package p2365_shortcoding_book;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
double r = scanner.nextDouble();// 半径
float x1 = scanner.nextFloat();
float y1 = scanner.nextFloat();
float a = x1;
float b = y1;
float sum = 0;
for (int i = 0; i < N - 1; i++) {
float x2 = scanner.nextFloat();
float y2 = scanner.nextFloat();
sum += dist(x2, y2, x1, y1);
x1 = x2;
y1 = y2;
}

sum += dist(a, b, x1, y1);

System.out.printf("%.2f", sum + (r * 2 * Math.PI));
}

static double dist(double x1, double y1, double x2, double y2) {
return Math.sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
}

}

: