이코테 0709 - 구현,그리디

HyeJi9908·2022년 7월 9일
0

[JAVA] ThisIsCodingTest

목록 보기
1/12

🔎 개념

Scanner 이용해 int 한 개씩 입력 받기

  • 입력

2 3 4
혹은
2
3
4

일 때, 공백을 기준으로 각각 입력 받기

		Scanner sc = new Scanner(System.in);

		// n,m,k 공백을 기준으로 변수에 입력 할당
		int n = sc.nextInt();
		int m = sc.nextInt();
		int k = sc.nextInt();
        
		//공백을 기준으로 배열에 입력 할당
		int[] arr = new int[n];
		for (int i = 0; i < n; i++)
			arr[i] = sc.nextInt();

Scanner 이용해 char 한 개 입력 받기

  • 입력

A

		char ch = (char)System.in.read();

🚫 주의)

A
B
C

위 처럼 문자가 하나여도 여러줄인 경우엔 nextLine() 써야함! ⬇

Scanner 이용해 char 한 줄 씩 입력 받기

= char 여러개 입력 받는 경우

		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		char a = str.charAt(0);
		char b = str.charAt(2);
        
        sc.close();

🚫 주의) nextInt()와 nextLine()을 연달아 쓰는 경우:
nextInt()는 숫자까지만 입력받은 뒤 바로 뒤에 nextLine()이 있다면
nextLine()은 개행문자 \n도 문자로 인식해버림
따라서 입력받을 숫자 nextInt()와 입력받을 문자열 nextLine() 사이 nextLine()을 한번 더 추가하여 \n 제거해줘야 함.


📚 그리디 - 큰 수의 법칙

import java.util.*;


public class Java0709_1 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);

		// n,m,k 공백을 기준으로 입력 받기
		int n = sc.nextInt();
		int m = sc.nextInt();
		int k = sc.nextInt();

		// n개의 수를 공백을 기준으로 구분해 하나씩 입력받기
		int[] arr = new int[n];
		for (int i = 0; i < n; i++)
			arr[i] = sc.nextInt();

		Arrays.sort(arr);
		int fisrt = arr[n - 1];
		int second = arr[n - 2];
		
		// ---  방법1) 공식을 도출해 계산
		int first_cnt = (m / (k + 1)) * k;
		first_cnt += m % (k + 1);
		// 가장 큰 수가 더해지는 횟수

		int answer = 0;
		answer += fisrt * first_cnt;
		answer += (m - first_cnt) * second;
		
//		// --- 방법2) 반복문 활용
//		while(true) {
//			for(int i=0;i<k;i--) {
//				if(m==0) break;
//				answer+= first;
//				m--;
//			}
//			if(m==0) break;
//			answer+=second;
//			m--;
//		}

		System.out.println(answer);
	}
}

📚 그리디 - 숫자 카드 게임

// 숫자 카드 게임
//	입력
//	3 3
//	3 1 2
//	4 1 4
//	2 2 2 

package ThisIsCodingTest;
import java.util.*;

public class Java0709_2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		int m = sc.nextInt();
		int answer =0;
		
		for (int i=0; i<n;i++) {
			int min = 10001;
			
			for(int j=0;j<m;j++) {
				int x = sc.nextInt();
				min = Math.min(min,x);
			} // 각 row내 가장 작은 값 도출
			
			answer = Math.max(answer, min);	
		}
		System.out.println(answer);
	}
}

📚 그리디 - 1로 만들기

import java.util.*;

public class Java0709_3 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n= sc.nextInt();
		int k= sc.nextInt();
		int answer =0;
		
		while(n>1) {
			if(n%k==0)
				n/=k;
			else 
				n--;
			
			answer++;
		}
		System.out.print(answer);
	}
}

📚 구현 - 상하좌우

import java.util.*;

public class Java0709_4 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n=sc.nextInt();
		sc.nextLine(); 	// nextInt()의 개행문자 제거용
		
		String[] arr=sc.nextLine().split(" "); 
		// 입력 한 줄을 한 번에 받아 배얄에 할당
		
		int x=1,y=1;
		int[][] d = {{-1,0},{1,0},{0,-1},{0,1}}; // 상하좌우
		char[] type = {'U','D','L','R'};
		
		for(String s:arr) {
			char word =s.charAt(0);
			int ny=0,nx=0;
			
			// if문 4개 대신 for문을 이용
			for(int w=0;w<4;w++) { 
				if(word == type[w]) {
					ny=y+d[w][0];
					nx=x=d[w][1];
				}
			}
			
			// 공간을 이탈하면
			if(ny<0||ny>=n||nx<0||nx>=n) 
				continue;
	
			x =nx;
			y =ny;
			
		}
		System.out.print(x+" "+y);
	}
}

📚 구현 - 시각

import java.util.*;

public class Java0709_5 {
	public static void main(String[] args) {
		Scanner sc =new Scanner(System.in);
		
		int n=sc.nextInt();
		int answer =0;
		
		int total=((n+1)*3600)-1; // n시 59분 59초를 모두 초로 바꿈
		int[] time=new int[3];
		
		while(total>=0) {
			
			time[0]= total/3600;
			time[1]=(total%3600)/60;
			time[2]=(total%3600%60);
			
			
			for(int i:time) {
				
				// 방법1) 문자열에 3 있는지 확인
				String str=String.valueOf(i);
				if(str.contains("3")) answer++;
				
//				// 방법2) 연산 이용
//				if(i/10==3||i%10==3) answer++;
			}
			
			total--;
		}
		
		System.out.print(answer);
	}
}

0개의 댓글