코딩테스트/백준(JAVA)

백준 코딩테스트 2-7-2480 주사위 세개 코딩 문제 java로 푸는 방법

쏠솔랄라 2023. 3. 11. 13:11

 

 

백준 코딩테스트 단계별로 풀어보기

2단계 조건문 7단계 2480 주사위 세 개 문제입니다

 

 

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

 

2480번: 주사위 세개

1에서부터 6까지의 눈을 가진 3개의 주사위를 던져서 다음과 같은 규칙에 따라 상금을 받는 게임이 있다.  같은 눈이 3개가 나오면 10,000원+(같은 눈)×1,000원의 상금을 받게 된다.  같은 눈이 2개

www.acmicpc.net

 

 

https://www.acmicpc.net/step

 

단계별로 풀어보기

단계별은 @jh05013님이 관리하고 계십니다. 단계제목설명정보총 문제내가 맞은 문제1입출력과 사칙연산입력, 출력과 사칙연산을 연습해 봅시다. Hello World!132조건문if 등의 조건문을 사용해 봅시다

www.acmicpc.net

 

 


 

 

 

 

문제
1에서부터 6까지의 눈을 가진 3개의 주사위를 던져서 다음과 같은 규칙에 따라 상금을 받는 게임이 있다. 
1. 같은 눈이 3개가 나오면 10,000원+(같은 눈)×1,000원의 상금을 받게 된다. 
2. 같은 눈이 2개만 나오는 경우에는 1,000원+(같은 눈)×100원의 상금을 받게 된다. 
3. 모두 다른 눈이 나오는 경우에는 (그 중 가장 큰 눈)×100원의 상금을 받게 된다.  

예를 들어, 3개의 눈 3, 3, 6이 주어지면 상금은 1,000+3×100으로 계산되어 1,300원을 받게 된다.
또 3개의 눈이 2, 2, 2로 주어지면 10,000+2×1,000 으로 계산되어 12,000원을 받게 된다.
3개의 눈이 6, 2, 5로 주어지면 그중 가장 큰 값이 6이므로 6×100으로 계산되어 600원을 상금으로 받게 된다.

3개 주사위의 나온 눈이 주어질 때, 상금을 계산하는 프로그램을 작성 하시오.

입력
첫째 줄에 3개의 눈이 빈칸을 사이에 두고 각각 주어진다. 

출력
첫째 줄에 게임의 상금을 출력 한다.

 

 

정답

import java.util.Scanner;

public class Main {

public static void main (String[] args) {

 

Scanner sc = new Scanner(System.in);

int a = sc.nextInt();

int b = sc.nextInt();

int c = sc.nextInt();

 

if (a==b && b==c) {

System.out.println(10000+a*1000);

} else if (a==b || a==c) {

System.out.println(1000+a*100);

} else if (b==c) {

System.out.println(1000+b*100);

} else {

if (a!=b && a!=c && b!=c) {

for (int i=6; i>0; i--) {

if (i==a || i==b || i==c) {

System.out.println(i*100);

break;

}

}

}

}

}

}

 

=>

문제를 잘 읽어야 하는 문제

첫째 줄에 숫자가 '주어진다'

Scanner로 3개 숫자를 입력받아 진행

 

// 처음에 스캐너 줄 생각 못 하고 랜덤주사위 돌린 방법

public class Main {

public static void main (String[] args) {

 

int a = (int)(Math.random()*6+1);

int b = (int)(Math.random()*6+1);

int c = (int)(Math.random()*6+1);

 

if (a==b && b==c) {

System.out.println(10000+a*1000);

} else if (a==b || a==c) {

System.out.println(1000+a*100);

} else if (b==c) {

System.out.println(1000+b*100);

} else {

if (a!=b && a!=c && b!=c) {

for (int i=6; i>0; i--) {

if (i==a || i==b || i==c) {

System.out.println(i*100);

break;

}

}

}

}

}

}

 

 


 

 

1-1-2557 Hello World https://developernew.tistory.com/41 <- 백준 답지 제출 방법
1-2-1000 A+B https://developernew.tistory.com/42
1-3-1001 A-B https://developernew.tistory.com/43
1-4-10998 AXB https://developernew.tistory.com/44
1-5-1008 A/B https://developernew.tistory.com/45
1-6-10869 사칙연산 https://developernew.tistory.com/46
1-7-10926 ??! https://developernew.tistory.com/47
1-8-18108 1998년생인 내가 태국에서는 2541년생?! https://developernew.tistory.com/48
1-9-10430 나머지 https://developernew.tistory.com/49
1-10-2588 곱셈 https://developernew.tistory.com/50
1-11-11382 꼬마 정민 https://developernew.tistory.com/51
1-12-10171 고양이 https://developernew.tistory.com/52
1-13-10172 개 https://developernew.tistory.com/53

2-1-1330 두 수 비교하기 https://developernew.tistory.com/54
2-2-9498 시험 성적 https://developernew.tistory.com/55
2-3-2753 윤년 https://developernew.tistory.com/56
2-4-14681 사분면 고르기 https://developernew.tistory.com/61
2-5-2884 알람 시계 https://developernew.tistory.com/68
2-6-2525 오븐 시계 https://developernew.tistory.com/69