본문 바로가기

코딩테스트

[JAVA] 기하: 직사각형과 삼각형

27323 직사각형

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.IOException;
//import java.io.BufferedWriter;
//import java.io.OutputStreamWriter;


public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int a = Integer.parseInt(br.readLine());
    int b = Integer.parseInt(br.readLine());

    System.out.println(a*b);

  }
}

 


1085 직사각형에서 탈출

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.IOException;
//import java.io.BufferedWriter;
//import java.io.OutputStreamWriter;


public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    StringTokenizer st = new StringTokenizer(br.readLine()," ");

    int x = Integer.parseInt(st.nextToken());
    int y = Integer.parseInt(st.nextToken());
    int w = Integer.parseInt(st.nextToken());
    int h = Integer.parseInt(st.nextToken());

    int x_min = Math.min(x, w-x);	// x축 최소거리
    int y_min = Math.min(y, h-y);	// y축 최소거리

    System.out.println(Math.min(x_min, y_min));

  }
}

Math.min 메서드를 사용했다.

 


3009 네 번째 점

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.IOException;
//import java.io.BufferedWriter;
//import java.io.OutputStreamWriter;


public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    StringTokenizer st1 = new StringTokenizer(br.readLine()," ");
    StringTokenizer st2 = new StringTokenizer(br.readLine()," ");
    StringTokenizer st3 = new StringTokenizer(br.readLine()," ");

    int x1 = Integer.parseInt(st1.nextToken());
    int x2 = Integer.parseInt(st2.nextToken());
    int x3 = Integer.parseInt(st3.nextToken());
    
    int y1 = Integer.parseInt(st1.nextToken());
    int y2 = Integer.parseInt(st2.nextToken());
    int y3 = Integer.parseInt(st3.nextToken());

    int x4=0;
    int y4=0;

    if (x1==x2) x4=x3;
    else if (x1==x3) x4=x2;
    else x4=x1;

    if (y1==y2) y4=y3;
    else if (y1==y3) y4=y2;
    else y4=y1;

    System.out.println(x4 + " " + y4);

  }
}

 


15894 수학은 체육과목 입니다

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.IOException;
//import java.io.BufferedWriter;
//import java.io.OutputStreamWriter;


public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    long n = Long.parseLong(br.readLine());

    System.out.println(n*4);

  }
}

처음에 맞게 제출했는데 틀렸다.

이유를 찾아보니...조건이 n<=10^9이여서 int형이 아닌 Long형으로 받아야한다!

함정이 있었네..

 


9063 대지

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.IOException;
//import java.io.BufferedWriter;
//import java.io.OutputStreamWriter;
import java.util.Arrays;


public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int n = Integer.parseInt(br.readLine());

    int[] x = new int[n], y = new int[n];

    for(int i = 0; i < n; i++) {
      StringTokenizer st = new StringTokenizer(br.readLine());
      x[i] = Integer.parseInt(st.nextToken());
      y[i] = Integer.parseInt(st.nextToken());
    }

    Arrays.sort(x);
    Arrays.sort(y);

    System.out.print((x[n - 1] - x[0]) * (y[n - 1] - y[0]));

  }
}

뭐라는거지...했는데 그냥 가장큰 x/y좌표에서 가장 작은 x/y좌표를 빼 가로/세로의 길이를 구한 다음 곱해서 대지의 넒이를 구하면된다.

 


10101 삼각형 외우기

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.IOException;
//import java.io.BufferedWriter;
//import java.io.OutputStreamWriter;
//import java.util.Arrays;


public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int a = Integer.parseInt(br.readLine());
    int b = Integer.parseInt(br.readLine());
    int c = Integer.parseInt(br.readLine());
    int sum=a+b+c;

    if (sum==180){
      if (a==b && b==c) System.out.println("Equilateral");
      else if (a==b || b==c || a==c) System.out.println("Isosceles");
      else System.out.println("Scalene");
    }
    else System.out.println("Error");

    br.close();
    

  }
}

 


5073 삼각형과 세 변

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.IOException;
//import java.io.BufferedWriter;
//import java.io.OutputStreamWriter;
import java.util.Arrays;


public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


    while(true){

      int[] n =new int[3];

      StringTokenizer st = new StringTokenizer(br.readLine(), " ");
      n[0] = Integer.parseInt(st.nextToken());
      n[1] = Integer.parseInt(st.nextToken());
      n[2] = Integer.parseInt(st.nextToken());

      if (n[0] == 0 && n[1]==0 && n[2]==0) break;

      Arrays.sort(n);
      if (n[2]>=n[0]+n[1]){
        System.out.println("Invalid");
        continue;
      }

      if (n[0]==n[1] && n[1]==n[2]) System.out.println("Equilateral");
      else if (n[0]==n[1] || n[1]==n[2] || n[0]==n[2]) System.out.println("Isosceles");
      else System.out.println("Scalene");

    }

    br.close();
  }
}

sort() 메서드 default는 오름차순 정렬이다.

 


14215 세 막대

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.IOException;
//import java.io.BufferedWriter;
//import java.io.OutputStreamWriter;
import java.util.Arrays;


public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


    int[] n =new int[3];

    StringTokenizer st = new StringTokenizer(br.readLine(), " ");
    n[0] = Integer.parseInt(st.nextToken());
    n[1] = Integer.parseInt(st.nextToken());
    n[2] = Integer.parseInt(st.nextToken());

    Arrays.sort(n);

    System.out.println(n[0]+n[1]+Math.min(n[0]+n[1]-1, n[2]));
  

    br.close();
  }
}

 


# 회고

 

이번 문제들은 다 쉬워서 괜찮게 풀었다.

'코딩테스트' 카테고리의 다른 글

[JAVA] 브루트 포스  (1) 2024.01.25
[JAVA] 시간 복잡도 단계  (0) 2024.01.24
[JAVA] 약수, 배수와 소수  (0) 2024.01.22
[JAVA] 일반 수학 1단계  (0) 2024.01.19
[JAVA] 2차원 배열  (0) 2024.01.18