ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1020. Rope

Correct Java Code
Posted by Rostam 6 Mar 2012 20:07
import java.text.DecimalFormat;
import java.util.Scanner;

public class P1020 {
    private static double pi = 3.14159;
    private static double totalDist;

    public static void main(String args[]) {

        DecimalFormat df = new DecimalFormat("#.##");

        Scanner in = new Scanner(System.in);
        int nRopes = in.nextInt();
        double radius = in.nextDouble();
        double[][] nodes = new double[nRopes][2];

        for (int i=0; i<nRopes; i++) {
            nodes[i][0] = in.nextDouble();
            nodes[i][1] = in.nextDouble();
        }

        totalDist = distance(nodes[0], nodes[nRopes-1])+pi*radius*2;
        for (int i=0; i < nRopes-1; i++) {
            totalDist += distance(nodes[i], nodes[i+1]);
        }

        System.out.println(df.format(totalDist));
    }

    static double distance (double arr1[], double arr2[]) {
        double distance = Math.sqrt((Math.pow((arr1[0]-arr2[0]), 2)+Math.pow((arr1[1]-arr2[1]), 2)));
        return distance;
    }
}
Re: Correct Java Code
Posted by Cheshire 10 Apr 2013 08:43
Can you say different between code? I have WA#4

import java.io.PrintWriter;
import java.util.Scanner;


public class p1020 {

    /**
     * @param args
     */
    static double line(double xy, double xy2,double xy3,double xy4){
        return (Math.sqrt(Math.pow(xy-xy3,2)+Math.pow(xy2-xy4,2)));
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int n = in.nextInt();
        double r = in.nextDouble();
        double pi = 3.14159;
        if (n==1) {
            out.println(Math.round(pi*r*200)/100.00);
            out.flush();
            return;
        }
        float[][] c = new float[n][2];
        for (int i =0; i < n; i++ ){
            c[i][0]=in.nextFloat();
            c[i][1]=in.nextFloat();
        }

        double res = pi *2 *r;
        for (int i =0; i < n-1; i++ ){
            res+=line(c[i][0],c[i+1][0],c[i][1],c[i+1][1]);
        }

        res+=line(c[0][0],c[n-1][0],c[0][1],c[n-1][1]);
        out.println(Math.round(res*100)/100.00);
        out.flush();
    }

}