์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Java

[BOJ/Step8] 2839 : ์„คํƒ• ๋ฐฐ๋‹ฌ (JAVA)

NaNaRin๐Ÿ™ƒ 2021. 1. 17. 16:01

www.acmicpc.net/problem/2839

 

2839๋ฒˆ: ์„คํƒ• ๋ฐฐ๋‹ฌ

์ƒ๊ทผ์ด๋Š” ์š”์ฆ˜ ์„คํƒ•๊ณต์žฅ์—์„œ ์„คํƒ•์„ ๋ฐฐ๋‹ฌํ•˜๊ณ  ์žˆ๋‹ค. ์ƒ๊ทผ์ด๋Š” ์ง€๊ธˆ ์‚ฌํƒ•๊ฐ€๊ฒŒ์— ์„คํƒ•์„ ์ •ํ™•ํ•˜๊ฒŒ Nํ‚ฌ๋กœ๊ทธ๋žจ์„ ๋ฐฐ๋‹ฌํ•ด์•ผ ํ•œ๋‹ค. ์„คํƒ•๊ณต์žฅ์—์„œ ๋งŒ๋“œ๋Š” ์„คํƒ•์€ ๋ด‰์ง€์— ๋‹ด๊ฒจ์ ธ ์žˆ๋‹ค. ๋ด‰์ง€๋Š” 3ํ‚ฌ๋กœ๊ทธ

www.acmicpc.net


๋ฌธ์ œ

์ƒ๊ทผ์ด๋Š” ์š”์ฆ˜ ์„คํƒ•๊ณต์žฅ์—์„œ ์„คํƒ•์„ ๋ฐฐ๋‹ฌํ•˜๊ณ  ์žˆ๋‹ค. ์ƒ๊ทผ์ด๋Š” ์ง€๊ธˆ ์‚ฌํƒ•๊ฐ€๊ฒŒ์— ์„คํƒ•์„ ์ •ํ™•ํ•˜๊ฒŒ Nํ‚ฌ๋กœ๊ทธ๋žจ์„ ๋ฐฐ๋‹ฌํ•ด์•ผ ํ•œ๋‹ค. ์„คํƒ•๊ณต์žฅ์—์„œ ๋งŒ๋“œ๋Š” ์„คํƒ•์€ ๋ด‰์ง€์— ๋‹ด๊ฒจ์ ธ ์žˆ๋‹ค. ๋ด‰์ง€๋Š” 3ํ‚ฌ๋กœ๊ทธ๋žจ ๋ด‰์ง€์™€ 5ํ‚ฌ๋กœ๊ทธ๋žจ ๋ด‰์ง€๊ฐ€ ์žˆ๋‹ค.

์ƒ๊ทผ์ด๋Š” ๊ท€์ฐฎ๊ธฐ ๋•Œ๋ฌธ์—, ์ตœ๋Œ€ํ•œ ์ ์€ ๋ด‰์ง€๋ฅผ ๋“ค๊ณ  ๊ฐ€๋ ค๊ณ  ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 18ํ‚ฌ๋กœ๊ทธ๋žจ ์„คํƒ•์„ ๋ฐฐ๋‹ฌํ•ด์•ผ ํ•  ๋•Œ, 3ํ‚ฌ๋กœ๊ทธ๋žจ ๋ด‰์ง€ 6๊ฐœ๋ฅผ ๊ฐ€์ ธ๊ฐ€๋„ ๋˜์ง€๋งŒ, 5ํ‚ฌ๋กœ๊ทธ๋žจ 3๊ฐœ์™€ 3ํ‚ฌ๋กœ๊ทธ๋žจ 1๊ฐœ๋ฅผ ๋ฐฐ๋‹ฌํ•˜๋ฉด, ๋” ์ ์€ ๊ฐœ์ˆ˜์˜ ๋ด‰์ง€๋ฅผ ๋ฐฐ๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

์ƒ๊ทผ์ด๊ฐ€ ์„คํƒ•์„ ์ •ํ™•ํ•˜๊ฒŒ Nํ‚ฌ๋กœ๊ทธ๋žจ ๋ฐฐ๋‹ฌํ•ด์•ผ ํ•  ๋•Œ, ๋ด‰์ง€ ๋ช‡ ๊ฐœ๋ฅผ ๊ฐ€์ ธ๊ฐ€๋ฉด ๋˜๋Š”์ง€ ๊ทธ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N์ด ์ฃผ์–ด์ง„๋‹ค. (3 ≤ N ≤ 5000)

 

์ถœ๋ ฅ

์ƒ๊ทผ์ด๊ฐ€ ๋ฐฐ๋‹ฌํ•˜๋Š” ๋ด‰์ง€์˜ ์ตœ์†Œ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ, ์ •ํ™•ํ•˜๊ฒŒ Nํ‚ฌ๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค๋ฉด -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ 1

18

 

์˜ˆ์ œ ์ถœ๋ ฅ 1

4

 

์˜ˆ์ œ ์ž…๋ ฅ 2

4

 

์˜ˆ์ œ ์ถœ๋ ฅ 2

-1

 

์˜ˆ์ œ ์ž…๋ ฅ 3

6

 

์˜ˆ์ œ ์ถœ๋ ฅ 3

2

 

์˜ˆ์ œ ์ž…๋ ฅ 4

9

 

์˜ˆ์ œ ์ถœ๋ ฅ 4

3

 

์˜ˆ์ œ ์ž…๋ ฅ 5

11

 

์˜ˆ์ œ ์ถœ๋ ฅ 5

3


ํ’€์ด

๊ฐ€์žฅ ์ ์€ ๊ฐœ์ˆ˜์˜ ๋ด‰์ง€๋ฅผ ์˜ฎ๊ธฐ๋ ค๋ฉด 5ํ‚ค๋กœ ๋ด‰์ง€๊ฐ€ ์ตœ๋Œ€ํ•œ ๋งŽ์•„์•ผ ํ•œ๋‹ค.

๋”ฐ๋ผ์„œ 5ํ‚ค๋กœ ๋ด‰์ง€๋ฅผ ๋จผ์ € ๊ณ„์‚ฐํ•˜๊ณ  ๋‚จ์€ ์–‘์„ 3ํ‚ค๋กœ ๋ด‰์ง€์— ๋‹ด์„ ์ˆ˜ ์žˆ๋Š”์ง€ ๊ณ„์‚ฐํ•œ๋‹ค.

1. ์ž…๋ ฅ๋ฐ›์€ n์„ 5๋กœ ๋‚˜๋ˆˆ ๊ฐ’์„ five์— ์ €์žฅ, 5๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ 3์œผ๋กœ ๋‚˜๋ˆˆ ๊ฐ’์„ three์— ์ €์žฅ

   ( five๋Š” 5ํ‚ค๋กœ ๋ด‰์ง€์˜ ๊ฐœ์ˆ˜, three๋Š” 3ํ‚ค๋กœ ๋ด‰์ง€์˜ ๊ฐœ์ˆ˜ )

2. 5 * five + 3 * three๋ฅผ total์— ์ €์žฅํ•˜๊ณ , total์ด ์ž…๋ ฅ๋ฐ›๋Š” n๊ณผ ๊ฐ™์•„์งˆ ๋•Œ๊นŒ์ง€ ๋ฌดํ•œ๋ฃจํ”„๋ฅผ ๋ˆ๋‹ค

3. 5ํ‚ค๋กœ ๊ฐœ์ˆ˜๋ฅผ ํ•˜๋‚˜ ์ค„์ด๊ณ  ( five-- ), ๋‚˜๋จธ์ง€๋ฅผ 3์œผ๋กœ ๋‚˜๋ˆ  three์— ์ €์žฅ, total์„ ๊ณ„์‚ฐํ•œ๋‹ค.

    ( ๋‚˜๋จธ์ง€๊ฐ€ 3์œผ๋กœ ๋‚˜๋ˆ ์ง€์ง€ ์•Š์œผ๋ฉด total์ด n๊ณผ ๊ฐ™์•„์ง€์ง€ ์•Š์Œ )

4. five๊ฐ€ 0๋ณด๋‹ค ์ž‘์•„์ง€๋ฉด ์ •ํ™•ํ•˜๊ฒŒ n์„ ๋งŒ๋“ค ์ˆ˜ ์—†์œผ๋ฏ€๋กœ -1์„ ์ถœ๋ ฅํ•˜๋„๋ก ๋ณ€์ˆ˜ ๊ฐ’์„ ์ˆ˜์ •ํ•œ๋‹ค.

 

import java.util.Scanner;

public class B2839 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		int five = n / 5;
		int three = n % 5 / 3;
		
		int total = 5 * five + 3 * three;
		
		while(total != n) {
			five--;
			three = ( n - (5 * five) ) / 3;
			total = 5 * five + 3 * three;
			if(five < 0) {
				five = 0;
				three = -1;
				break;
			}
		}	
		System.out.println(five + three);
	}
}