[백준] 2480번 : 주사위 세 개 - JAVA(자바)

Life is ninanino·2022년 10월 4일
0

[백준] JAVA

목록 보기
2/37
post-thumbnail

https://www.acmicpc.net/problem/2480



미친 하드코딩....

package 백준;// @ author ninaaano

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class 주사위세개 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer str = new StringTokenizer(br.readLine()," ");
        int one = Integer.parseInt(str.nextToken());
        int two = Integer.parseInt(str.nextToken());
        int three = Integer.parseInt(str.nextToken());
        br.close();

        int gold = 0;

        if(one==two || one==three){
            gold = 1000+one*100;
            if(two==three){
                gold = 10000+one*1000;
            }
        }else if(two==three){
            gold = 1000+two*100;
            if(one==three){
                gold = 10000+two*1000;
            }
        }else
            if(one>two && one>three){
                gold = one*100;
            }else if(two>one && two>three){
                gold = two*100;
            }else if(three> one && three>two)
                gold = three*100;

        System.out.print(gold);
    }
}

이렇게 푸는게 맞는걸까? 이게 최선일까..?
사이트에 나와있는 테스트가 다 돌아가서 오 정답인가 했는데
오류가 있었다
마지막 부분을 ||로 생각했더니 틀려서 &&으로 바꿔줬다
좀 더 깔끔하게 푸는 방법이 있을 것 같은데.. 너무 신난다 돌아간다만 한걸지도ㅠㅠ

++ 다른풀이

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
 
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 a, b, c;
		a = Integer.parseInt(st.nextToken());
		b = Integer.parseInt(st.nextToken());
		c = Integer.parseInt(st.nextToken());
 
		// 만약 모든 변수가 다른 경우
		if (a != b && b != c && a != c) {
			int max = Math.max(a, Math.max(b, c));
			System.out.println(max * 100);
		}
		// 3개의 변수가 모두 같은 경우
		else if (a == b && a == c) {
			System.out.println(10000 + a * 1000);
		}
		// a가 b혹은 c랑만 같은 경우
		else if(a == b || a == c) {
			System.out.println(1000 + a * 100);
		}
		// b가 c랑 같은 경우
		else {
			System.out.println(1000 + b * 100);
		}
	}
}

출처 : st님 티스토리

아.... != 를 잊고있었다....

++ 백준 숏코딩 랭크

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int A=sc.nextInt(),B=sc.nextInt(),C=sc.nextInt();
		int P;
		if (A==B){
			if(B==C) P=(A+10)*1000;
			else P=(A+10)*100;
		}
		else if (B==C || C==A) P=(C+10)*100;
		else P=Math.max(Math.max(A,B), C)*100;
		System.out.print(P);
	}
}

작성자 : brackman007

이게 7년전 코드라니....

++ 추가 풀이 코드

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a=sc.nextInt(), b=sc.nextInt(), c=sc.nextInt(), ans=0;
		if(a==b && b==c)
			ans=10000+a*1000;
		else if(a==b || b==c)
			ans=1000+b*100;
		else if(c==a)
			ans=1000+a*100;
		else
			ans=Math.max(Math.max(a, b),c)*100;
		System.out.println(ans);
	}
}

작성자 : hunu_cho

여기서 Math.max() 란?
Math.max()메서드는 자바의 기본 java.lang.Math클래스 안의 메서드 중 하나다
두 개의 인자를 비교하여 큰 값을 리턴해준다
오로지 숫자(정수, 실수)만 비교가 가능하며 문자열은 비교가 불가능하다
두 값이 같은 경우는 동일한 값을 리턴해준다

이런 방법이 있었구나....

profile
백엔드 프로그래밍을 공부하고 있습니다. AWS, 클라우드 환경에 대해 관심이 많습니다.

0개의 댓글