알고리즘
[백준] 2839 - 설탕 배달
JH_Lucid
2021. 6. 11. 00:13
- 수학적으로 푸는 방법 .. 대단
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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());
if (n == 4 || n == 7) {
System.out.println(-1);
} else if ((n % 5) == 1 || (n % 5) == 3) {
System.out.println(n / 5 + 1);
} else if ((n % 5) == 2 || (n % 5) == 4) {
System.out.println(n / 5 + 2);
} else
System.out.println(n / 5);
}
}
- 일반적으로 푸는 방법.
-> 5로 나뉘면 출력하고, 안 나뉘면 나뉠때까지 3으로 뺀다.
import java.util.Scanner;
public class Main {
public static void main(String args[]){
int input = 0;
int count = 0;
Scanner sc = new Scanner(System.in);
input = sc.nextInt();
while(true) {
if (input % 5 ==0) {
System.out.println(input/5 + count);
break;
}else if(input < 0) {
System.out.println(-1);
break;
}
input = input-3;
count++;
}
}
}