백준 2798번 (브루트 포스)
import java.util.Scanner;
public class problem68 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int size = in.nextInt();
int max = in.nextInt();
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = in.nextInt();
}
int result = result(array, size, max);
System.out.println(result);
}
static int result(int[] arr, int a, int b) {
// 초기값
int temp = 0;
// 0 ~ 3 3장
for (int i = 0; i < a - 2; i++) {
// 1 ~ 4 3장
for (int j = i + 1; j < a - 1; j++) {
for (int k = j + 1; k < a; k++) {
int total = arr[i] + arr[j] + arr[k];
// 입력값 : 5 6 7 8 9
// 5 6 7 = 19 // temp 19
// 5 7 8 = 20
if (total == b) {
return total;
}
if (temp < total && total < b) {
temp = total;
}
}
}
}
return temp;
}
}
백준 11047번 (그리디 알고리즘)
import java.util.Scanner;
public class problem69 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int size = in.nextInt();
int price = in.nextInt();
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = in.nextInt();
}
int money = price;
int count = 0;
// 오름차순으로 입력받으니까 배열 뒷부분이 가장큰 금액
for (int i = array.length - 1; i >= 0; i--) {
// 배열의 값보다 money 값이 크거나 같은경우
if(array[i] <= money){
count += money / array[i];
money = money % array[i];
}
}
System.out.println(count);
}
}
백준 11399번 (그리디 알고리즘)
import java.util.Arrays;
import java.util.Scanner;
public class problem70 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int size =in.nextInt();
int [] array = new int[size];
for(int i = 0; i < size; i++){
array[i] = in.nextInt();
}
Arrays.sort(array);
int sum = 0;
int total = 0;
for(int i = 0; i <array.length; i++){
// 1 + 2 + 3 + 4 + 5
sum += array[i];
// 덧셈을 중첩 해주는 값
// 1 + 3 + 6 + 10 + 15
total += sum;
}
System.out.println(total);
}
}
프로그래머스 저주 받은 숫자 3 (Level 0)
class problem71 {
public int solution(int n) {
int answer = 0;
for(int i = 1; i <= n; i++) {
answer++;
// 31같은경우를 생각해줘야된다,
//31 / 10 % 10 = 3임으로 whlie조건에 걸려들어간다.
while(answer % 3 == 0 || answer % 10 == 3 ||
answer / 10 % 10 == 3) {
answer++;
}
}
return answer;
}
}
백준 1026번 (그리디 알고리즘)
import java.util.Arrays;
import java.util.Scanner;
public class problem72 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int size = in.nextInt();
int[] a = new int[size];
int[] b = new int[size];
for (int j = 0; j < size; j++) {
a[j] = in.nextInt();
}
for (int j = 0; j < size; j++) {
b[j] = in.nextInt();
}
Arrays.sort(a);
Arrays.sort(b);
int reverseA[] = new int[a.length];
int k = 0;
// 내림차순 정렬
for (int i = a.length - 1; i >= 0; i--) {
reverseA[k] = a[i];
k++;
}
int total = 0;
for (int i = 0; i < size; i++) {
total += reverseA[i] * b[i];
}
System.out.println(total);
}
}
백준 5585번 (그리디 알고리즘)
import java.util.Scanner;
public class problem73 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int[] arr = {500, 100, 50, 10, 5, 1};
int price = 1000 - a;
int count = 0;
for (int i = 0; i < arr.length; i++) {
// 잔돈이 arr 배열보다 클경우 로직을 통해 갯수를 더해준다.
if (arr[i] <= price) {
count += price / arr[i];
price %= arr[i];
}
}
System.out.println(count);
}
}
백준 10162번 (그리디 알고리즘)
import java.util.Scanner;
public class problem74 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 5 1 10
int[] arr = {300, 60, 10};
int time = in.nextInt();
int[] total = new int[arr.length];
for (int i = 0; i < 1; i++) {
// 시간에 맞지않은 값이 입력되었을경우
if (time % 10 != 0) {
System.out.print(-1);
break;
}
if (time >= arr[i]) {
total[i] = time / arr[i];
time %= arr[i];
}
if (time >= arr[i + 1]) {
total[i + 1] = time / arr[i + 1];
time %= arr[i + 1];
}
if (time >= arr[i + 2]) {
total[i + 2] = time / arr[i + 2];
time %= arr[i + 2];
}
for (int j : total) {
System.out.print(j + " ");
}
}
}
}