(SPOJ) Marciano - Solution

Link to the problem: http://br.spoj.com/problems/MARCIAN1/

For this problem, we need to use some mathematical concepts.


import java.io.*;
import java.util.*;

class Main {
    public void process() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
        String line = br.readLine();
        String[] s = line.split("\\s");
        int l = Integer.parseInt(s[0]);
        int a = Integer.parseInt(s[1]);
        int p = Integer.parseInt(s[2]);
        int r = Integer.parseInt(s[3]);
       
        int maiorLado = Math.max(l, a);
        maiorLado = Math.max(maiorLado, p);
       
        boolean serve = false;
        if (maiorLado/2 <= r) {
            double metadeMaiorLado = (double)maiorLado/2;
            double metadeLadoEsferico = Math.sqrt(Math.pow(r, 2)-Math.pow(metadeMaiorLado, 2));
            int ladoEsferico = (int)metadeLadoEsferico*2;
           
            if (maiorLado == l) {
                if (ladoEsferico >= a && ladoEsferico >= p) {
                    serve = true;
                }
            }
            else if (maiorLado == a) {
                if (ladoEsferico >= l && ladoEsferico >= p) {
                    serve = true;
                }
            }
            else { // maiorLado == p
                if (ladoEsferico >= l && ladoEsferico >= a) {
                    serve = true;
                }
            }
        }
        if (serve) {
            System.out.println("S");
        }
        else {
            System.out.println("N");
        }
        
        return;
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
       
        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução