카테고리 없음

[지코바] 5회차 문제풀이

moonseok 2022. 3. 24. 22:21

1. 수 정렬하기2

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

public class sortNum2 {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		int N = Integer.parseInt(br.readLine());
		int arr[] = new int[N];
		for(int i = 0; i < N; i++) {
			arr[i] = Integer.parseInt(br.readLine());
		}
		
		Arrays.sort(arr);
		for(int j = 0; j < N; j++) {
			sb.append(arr[j]).append('\n');
		}
		System.out.println(sb);
	}
}

2. 부녀회장이 될테야

package jicoba;
import java.util.Scanner;

public class bansang {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int TestCase;
		int floor;
		int number;
		int[][] Apartment = new int[15][15];
		
		for(int i = 0; i < 15; i++) {
			Apartment[i][1] = 1;
			Apartment[0][i] = i;
		}
		
		for(int j = 1; j < 15; j++) {
			for(int k = 2; k < 15; k++) {
				Apartment[j][k] = Apartment[j][k-1] + Apartment[j-1][k];
			}
		}
		
		System.out.print("TestCase : ");
		TestCase = sc.nextInt();
		for(int i = 0; i < TestCase; i++) {
			System.out.print("층수 : ");
			floor = sc.nextInt();
			System.out.print("호수 : ");
			number = sc.nextInt();
			System.out.println(floor + "층 " + number + "호실에는 " + Apartment[floor][number] + "명이 살고있습니다.");	
		}
	}
}

3. 블랙잭

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

public class black_Jack {
	static int search(int[] array, int a, int b) {
		int result = 0;
		
		for(int i = 0; i < a-2; i++) {
			for(int j = i+1; j < a-1; j++) {
				for(int o = j+1; o < a; o++) {
					int sum = array[i] + array[j] + array[o];
					if(b == sum) 
						return sum;
					if(result < sum && sum < b)
						result = sum;
				}
			}
		}
		return result;
	}
    
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine()," ");
		
		int N = Integer.parseInt(st.nextToken());
		int M = Integer.parseInt(st.nextToken());
		
		int [] arr = new int [N];
		
		st = new StringTokenizer(br.readLine(), " ");
		for(int i = 0; i < N; i++) {
			arr[i] = Integer.parseInt(st.nextToken());
		}
		int result = search(arr, N ,M);
		System.out.println(result);
	}
}

4. 나무 자르기

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

public class wood_cutter {
		public static void main(String[] args) throws IOException {
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			StringTokenizer st = new StringTokenizer(br.readLine()," ");
			
			int N = Integer.parseInt(st.nextToken());
			int M = Integer.parseInt(st.nextToken());
				
			Integer arr[] = new Integer[N];
			st = new StringTokenizer(br.readLine(), " ");
				
			for(int i = 0; i < N; i++) {
				arr[i] = Integer.parseInt(st.nextToken());
			}
			
			Arrays.sort(arr,Collections.reverseOrder());
			
			int start = 0;
			int end = arr[0]; 

			while (start < end) {
				  int mid = (start + end) / 2;
				  long sum = 0;
			
				  for (int i : arr) {
					  if (i - mid > 0) {
						  sum += i - mid;
					  }
				  }
				
				  if (sum < M) {
					  end = mid;
				  } else {
					  start = mid + 1;
				  }
		    	}
			System.out.println(end-1);
		}

}

 


5. 설탕 배달

import java.util.Scanner;

public class sugar {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int div = Math.floorDiv(N,5);
		int res = 0;
		
		if(N >= 3) {
			if(N % 5 == 0) {
				res = N/5;
				
			}else if(N % 5 == 4){
				if(div - 2 >= 0) {
					res = (div-2) + 4;
				}else if(div - 2 == -1){
					res = 3;
				}else {
					res = -1;
				}
				
			}else if(N % 5 == 3){
				res =  div +1;
				
			}else if(N % 5 == 2) {
				if(div-2 >=0) {
					if((N - ((div-2) * 5)) % 3  == 0) {
						res = ((N - ((div-2) * 5)) / 3)  + (div-2);
					}else {
						res = -1;
					}
				}else {
					res = -1;
				}
				
			}else {
				res = ((N - ((div-1) * 5)) / 3) + (div-1);
			}
			
		}else {
			res = -1;
		}
		System.out.println(res);
	}
}