728x90
반응형
728x90
반응형

문제는 다음과 같습니다.

https://www.acmicpc.net/problem/8958


 
import java.util.Scanner;

public class Test8958 {
	public static void main(String[] args) { 
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
		
		for(int i=0; i<n; i++) {
			String input = sc.nextLine();
			int result=0;
			int score=0;
			for(int j=0; j<input.length(); j++) {
				if(input.charAt(j)=='O') {
					score++;
					result +=score;
				}else {
					score=0;
				}
			}
			System.out.println(result);
		}
		 	
	}
}
 





결과는 다음과 같습니다.


728x90
반응형
728x90
반응형

문제는 다음과 같습니다.

https://www.acmicpc.net/problem/2577

  
import java.util.Scanner;

public class Test2577 {
    public static void main(String[] args) { 
        Scanner sc = new Scanner(System.in);
        int num[] = new int[10];
        int array[] = new int[3];
        int result = 1;
	    
        for(int i=0; i<3; i++){
            array[i] = sc.nextInt();
        }
        result = array[0] * array[1] * array[2];
        //System.out.println(result);

        while(result>0) {
            int chk = result % 10; //나머지를 chk번째 배열에 넣어 1씩 추가
            result = result / 10;
            num[chk] += 1;
        }
	    
        for(int i=0; i<num.length; i++){
            System.out.println(num[i]);
        }
		 	
    }
}
 




결과는 다음과 같습니다.


728x90
반응형
728x90
반응형

문제는 다음과 같습니다.

https://www.acmicpc.net/problem/1149


 
import java.util.Scanner;

public class Test1149 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
	    int N = scanner.nextInt();
	    int[][] cost = new int[3][N];
	    int r, g, b;
	     
	    cost[0][0] = scanner.nextInt();
	    cost[1][0] = scanner.nextInt();
	    cost[2][0] = scanner.nextInt();
	     
	    for(int i = 1; i < N; i++) {
	      r = scanner.nextInt();
	      g = scanner.nextInt();
	      b = scanner.nextInt();
	       
	      cost[0][i] = r + Math.min(cost[1][i-1], cost[2][i-1]);
	      cost[1][i] = g + Math.min(cost[0][i-1], cost[2][i-1]);
	      cost[2][i] = b + Math.min(cost[0][i-1], cost[1][i-1]);
	    }
	    System.out.println(Math.min(cost[0][N-1], Math.min(cost[1][N-1], cost[2][N-1])));
	    /*for(int i=0; i<N; i++) {
	    	for(int j=0; j<N; j++) {
	    		System.out.print(cost[i][j]+" ");
	    	}System.out.println();
	    }*/
	    scanner.close();
	}
}
 




결과는 다음과 같습니다.


728x90
반응형
728x90
반응형

카카오톡 모의 문제입니다.

1번

 
import java.util.Scanner;

public class Test {
/*	문제1
	자연수 N이 주어지면, N의 각 자릿수의 합을 구해서 return 하는 solution 함수를 만들어 주세요.
	예를들어 N = 123이면 1 + 2 + 3 = 6을 return 하면 됩니다.

	제한사항 
	N의 범위 : 100,000,000 이하의 자연수
*/
	public static int solution(int n) {
		int answer = 0;
        while(n>0){
            answer += (n%10);
            n = n/10;
        }
		return answer;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		System.out.println("자릿수의 합 : " +solution(a));
	}

}
 

2번

 
import java.util.Scanner;

public class Test {
/* 문제2	
 * 길이가 n인 배열에 1부터 n까지 숫자가 중복 없이 한 번씩 들어 있는지를 확인하려고 합니다.
	1부터 n까지 숫자가 중복 없이 한 번씩 들어 있는 경우 true를, 아닌 경우 false를 반환하도록 함수 solution을 완성해주세요.

	제한사항
	배열의 길이는 10만 이하입니다.
	배열의 원소는 10만 이하의 자연수입니다.
*/
	public static boolean solution(int[] arr) {
        boolean answer = true;
        int[] chk = new int[arr.length+1];
        for(int i=0; i<arr.length; i++) {
        	if(arr[i]<1 || arr[i]>arr.length) { //n이하의 자연수여야 함으로 arr[i]값이 arr.length보다 크면 false
        		answer = false;
        		return answer;
        	}
        	chk[arr[i]]++; //chk배열을 통해 n이하의 자연수가 있는 지 확인
        }
        for(int i=1; i<=arr.length; i++) {
        	if(chk[i]>1) {
        		answer = false;
        		return answer;
        	}
        }
        return answer;	
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int arr[] = new int[a];
		for(int i=0; i<a; i++) {
			arr[i] = (int) (Math.random()*10)+1;
			//System.out.print(arr[i]+ " ");
		}
		System.out.println("결과는 " +solution(arr));
	}

}
 

3번

 
public class Test {
/* 문제3	
 * 직사각형을 만드는 데 필요한 4개의 점 중 3개의 좌표가 주어질 때, 나머지 한 점의 좌표를 구하려고 합니다. 
 * 점 3개의 좌표가 들어있는 배열 v가 매개변수로 주어질 때, 직사각형을 만드는 데 필요한 나머지 한 점의 좌표를 return 하도록 solution 함수를 완성해주세요. 
 * 단, 직사각형의 각 변은 x축, y축에 평행하며, 반드시 직사각형을 만들 수 있는 경우만 입력으로 주어집니다.
 * 
 * 제한사항
	v는 세 점의 좌표가 들어있는 2차원 배열입니다.
	v의 각 원소는 점의 좌표를 나타내며, 좌표는 [x축 좌표, y축 좌표] 순으로 주어집니다.
	좌표값은 1 이상 10억 이하의 자연수입니다.
	직사각형을 만드는 데 필요한 나머지 한 점의 좌표를 [x축 좌표, y축 좌표] 순으로 담아 return 해주세요.
*/
	public int[] solution(int[][] v) {
        int[] answer = {0,0};
        for(int i=0; i<3; i++) { //3개의 점에 대해 수행하기에 반복문을 3번 돌린다.
        	answer[0] ^= v[i][0]; //x좌표는 주어진 좌표에 x좌표를 xor하고
        	answer[1] ^= v[i][1]; //y좌표는 주어진 좌표에 y좌표를 xor하면 나머지 한 점의 좌표가 들어가게 된다.
        }
        return answer;
    }
}
 

4번

 
/*문제4 
   * 1와 0로 채워진 표(board)가 있습니다. 표 1칸은 1 x 1 의 정사각형으로 이루어져 있습니다. 
	 * 표에서 1로 이루어진 가장 큰 정사각형을 찾아 넓이를 return 하는 solution 함수를 완성해 주세요. 
	 * (단, 정사각형이란 축에 평행한 정사각형을 말합니다.)
		
	제한사항
		표(board)는 2차원 배열로 주어집니다.
		표(board)의 행(row)의 크기 : 1000 이하의 자연수
		표(board)의 열(column)의 크기 : 1000 이하의 자연수
		표(board)의 값은 1또는 0으로만 이루어져 있습니다.
	*/
	int dp[][] = new int[1001][1001];
	public int solution(int[][] board){
		int answer=0;
	    for(int i=1; i<=board.length; i++){
	    	for(int j=1; j<=board[0].length; j++){
	    		if(board[i-1][j-1] != 0){
	    			dp[i][j] = Math.min(dp[i][j-1], Math.min(dp[i-1][j], dp[i-1][j-1])) + 1;
	                answer = Math.max(answer, dp[i][j]);
	            }
	        }
	    }
	    return answer*answer;
	}
}
 
728x90
반응형
728x90
반응형

문제는 다음과 같습니다. 

https://www.acmicpc.net/problem/11053

문제 이해부터가 힘드네요....

가장 긴 증가하는 부분 수열이라....

주어진 수열 중에서 증가하는 수열의 길이를 가장 길게 만들어라??라는 의미인데요.




  
import java.util.Scanner;
 
public class Test11053 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //수열의 크기 N, N크기의 수열 arr[], 증가하는 부분 수열의 길이 dp[i]
        int N = sc.nextInt();
        int arr[] = new int[N];
        for(int i=0; i<N; i++){
        arr[i] = sc.nextInt();
        }
        int max=1;
        int dp[] = new int[N];
        for(int i=0; i<N; i++){
            dp[i]=1;
            for(int j=0; j<i; j++){
                if(arr[i]>arr[j] && dp[i]<=dp[j]){
                    dp[i]+=1;
                    if(max<dp[i]){
                        max = dp[i];
                    }
                }
            }
        }
        System.out.println(max);
        sc.close();
    }
}
 


결과는 다음과 같습니다.



728x90
반응형
728x90
반응형

문제는 다음과 같습니다.

https://www.acmicpc.net/problem/6359



  
import java.util.Scanner;

public class Test6359 {
    public static void main(String[] args) {
        //테스트 수 T, 방의개수 n, 방 room[], 열린 방의 수 cnt,라운드 round, 문 상태 door 
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int i=0; i<T; i++){
            int cnt=0;
            int n = sc.nextInt();
            boolean room[] = new boolean[n];
            for(int round=1; round<=n; round++){
                for(int door=0; door<n; door++){
                    if((door+1)%round==0){
                        if(room[door]==false){
                            cnt++;
                            room[door]=true;
                        }else{
                            cnt--;
                            room[door]=false;
                        }
                    }
                }
            }
        System.out.println(cnt);
        }
        sc.close();
    }
}
 


결과는 다음과 같습니다.


728x90
반응형
728x90
반응형

문제는 다음과 같습니다. 

https://www.acmicpc.net/problem/9095


 
import java.util.Scanner;
public class Test9095 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt(); //방법의 수를 테스트할 갯수
        int[] arr = new int[11];
		
        arr[1]=1; 
        arr[2]=2;
        arr[3]=4;
        //경우의 수 추출
        for(int i=0; i<T; i++){
            int n = sc.nextInt();
            for(int j=4; j<=n; j++){
                arr[j]=arr[j-3]+arr[j-2]+arr[j-1];
            }
            System.out.println(arr[n]);
        }

    }
}
 


결과는 다음과 같습니다.


728x90
반응형
728x90
반응형

문제는 다음과 같습니다.

https://www.acmicpc.net/problem/11399



 
import java.util.Scanner;

public class ATMTest {
	public static void main(String[] args){
		System.out.println("백준알고리즘 11399번 ATM");
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt(); //인원 수 입력
		int[] people = new int[N]; //배열 생성
		for(int i=0; i<N; i++){
			people[i] = sc.nextInt();
		}
		for(int i=0; i<people.length; i++){ //정렬
			for(int j=i+1; j<people.length; j++){
				if(people[i]>people[i+1]){
					int imsi = people[i];
					people[i] = people[i+1];
					people[i+1] = imsi;
				}
			}
		}
		int min=0;
		for(int i=0; i<people.length; i++){
			for(int j=0; j<=i; j++){
				min += people[j];
			}
		}
		System.out.println(min);
	}
}
 

결과는 다음과 같습니다.



728x90
반응형
728x90
반응형

+ Recent posts