문제 : https://www.acmicpc.net/problem/1874


r = int(input())
stack =[]
chk = []
result = []
# 기본 세팅
for i in range(r) :
chk.append(int(input()))
idx = 0
for i in range(1,r+1) :
stack.append(i)
result.append("+")

while idx < r and len(stack) != 0 and chk[idx] == stack[-1] :
stack.pop()
result.append("-")
idx = idx + 1

if not stack :
for i in result :
print(i)
else :
print("NO")


문제 : https://www.acmicpc.net/problem/10430


input=input().split()
a = int(input[0])
b = int(input[1])
c = int(input[2])
print((a+b)%c)
print((a%c+b%c)%c)
print((a*b)%c)
print((a%c * b%c))


 

문제:  https://www.acmicpc.net/problem/15683



시험에서는 참조값 초기화에서 버벅거려서 DFS까지 다 구현해놓고 에러를 찾다가 제시간에 제출하지 못했다 ....또륵.,.

소 잃고 외양간이라도 고쳐본다...


백준 사이트에 올린 소스코드에는 주석이 달려있으니 코드보고 이해가 안간다면 주석을 풀어가면서 실행해보면 이해가 빠를 거에요!



백준 사이트에 제출한 소스 코드 : https://www.acmicpc.net/source/8390315


 import java.util.LinkedList;
import java.util.Scanner;

public class Main {
    
    static LinkedList cctv = new LinkedList<>();
    static int min;
    static int col;
    static int row;
    static int [][] map;
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
            col = scan.nextInt();
            row = scan.nextInt();
            min = col * row;
            map = new int[col][row]; 
            boolean [][] chk_map = new boolean[col][row];
            
            for(int i = 0 ; i < col ; i++) {
                for(int j = 0 ; j < row; j ++) {
                    map[i][j] = scan.nextInt();
                    if(map[i][j] > 0 && map[i][j] < 6) {
                        cctv.add(new Node(map[i][j],i,j));
                    }
                    if(map[i][j] ==6){
                        chk_map[i][j] = true;
                    }
                }
            }
            search(0,chk_map);
            System.out.println(min);
        scan.close();
    }
    
    public static void search(int start, boolean[][] arr) {
        
        boolean[][] resArr = new boolean[col][row];
       
        if (start >= cctv.size() ) {
            int count = count(arr);
            if(min > count) {
                min = count;
            }
            return ;
        }
        
        Node n = cctv.get(start);
        
        switch (n.value) {
        case 1: 
//            System.out.println("type : " + n.value);
            for(int i =  0; i < 4; i++) {

                for(int z = 0 ; z < col ; z++) {
                    for( int j = 0 ; j = 0 ; i--) {
                if(map[i][node.row] == 6) {
                    break;
                }
                    arr[i][node.row] = true;
            }
            
            break;
        case 1 : 
//            System.out.println("하");
            for(int i = node.col ; i < col ; i++) {
                if(map[i][node.row] == 6) {
                    break;
                }
                    arr[i][node.row] = true;
            }

            break;
        case 2 : 
//            System.out.println("좌");
            for(int i = node.row ; i >= 0 ; i--) {
                if(map[node.col][i] == 6) {
                    break;
                }
                    arr[node.col][i] = true;
            }
            break;
            
        case 3 : 
//            System.out.println("우");
            for(int i = node.row ; i < row; i++) {
                if(map[node.col][i] == 6) {
                    break;
                }
                    arr[node.col][i] = true;
            }
            
            break;
        }
        return arr;
    }
    
    static class Node {
        int value;
        int col;
        int row;
        public Node(int v, int c, int r) {
            value = v ;
            col = c;
            row = r;
        }
    }
}

문제 : https://www.acmicpc.net/problem/2606



import java.util.Scanner;

public class Main {
    static int vCnt;
    static int pair_cnt;
    
    static int [][] vMap;
    static int [] chkVisited;
    
    static int count;
    
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        vCnt = scan.nextInt();
        pair_cnt = scan.nextInt();
        
        vMap = new int[vCnt+1][vCnt+1];
        chkVisited = new int [vCnt+1];
        count = 0;
        //인접행렬 만들기
        for (int i = 0; i < pair_cnt; i++) {
            int a = scan.nextInt();
            int b = scan.nextInt();
            
            vMap[a][b] = vMap[b][a] =1 ;
            
        }
        
/*        for (int i = 0; i < vMap.length ; i++) {
            for (int j = 0 ; j < vMap[i].length ; j++) {
                System.out.print(vMap[i][j]);
            }
            System.out.println();
        }*/
        
        dfs(1);
        System.out.println(count);
    }
    
    public static void dfs (int start) {
        chkVisited[start] = 1;
        
        for(int i = 1 ; i < chkVisited.length ; i ++) {
            if(vMap[start][i] == 1 && chkVisited[i] == 0) {
//                System.out.println(start +"->"+ i);
                count ++;
                dfs(i);
            }
        }
    }
}

문제 : https://www.acmicpc.net/problem/9012


import java.util.Scanner;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int repeat = Integer.parseInt(scan.next());
        for (int j = 0; j < repeat; j++) {

            String input = scan.next();
            String res = "YES";
            Stack st = new Stack<>();
            for (int i = 0; i < input.length(); i++) {
                if (input.charAt(i) == '(') {
                    st.push(1);
                } else if (input.charAt(i) == ')') {
                    if (st.isEmpty()) {
                        res = "NO";
                        break;
                    } else {
                        st.pop();
                    }
                }

            }
            if(!st.isEmpty()) {
                res = "NO";
            }
            System.out.println(res);
        }


    }
}

문제 : https://www.acmicpc.net/problem/10828


import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new  Scanner(System.in);
        InnerStack s = new InnerStack();
        int input = Integer.parseInt(scan.next());
        
        for(int i = 0 ; i < input ; i++) {
            String str = scan.next();
            if("push".equals(str)) {
                int num = Integer.parseInt(scan.next());    
                s.push(num);
            }else if("pop".equals(str)) {
                System.out.println(s.pop());
            }else if("size".equals(str)) {
                System.out.println(s.size());
            }else if("empty".equals(str)) {
                System.out.println(s.isEmpty());
            }else if("top".equals(str)) {
                System.out.println(s.top());
            }    
            
        }
    }
    
    public static class InnerStack {
        InnerStack(){
        }
        List list = new LinkedList<>();
        private int cursor = 0 ;
    
        public void push(int i ) {
            list.add(i);
            cursor++;
        }
        
        public int pop() {
            int res;
            
            if(cursor > 0) {
                res=list.get(cursor-1);
                list.remove(cursor-1);
                cursor--;
            }else {
                res = -1;
            }
            
            return res;
        }
        
        public int top() {
            int res;
            if(cursor > 0) {
                res=list.get(cursor-1);
            }else {
                res = -1;
            }
            return res;
        }
        
        public int size() {
            return cursor;
        }
        
        public int isEmpty() {
            int res;
            if (cursor ==0) {
                res = 1;
            }else {
                res = 0;
            }
            return res;
        }
        
    }
}

문제 : https://www.acmicpc.net/problem/2750


import java.util.Scanner;

public class Main {
    static int[] bubleSort(int[] array) {
        int[] result = new int[array.length];
            for(int i = array.length ; i > 1 ; i -- ) {
                for(int j = 0 ; j < i-1 ; j++ ) {
                    if(array[j]>array[j+1]) {
                        int temp = array[j+1];
                        array[j+1] = array[j];
                        array[j] = temp;
                    }
                    
                }
            }
        result = array;
        return result;
    }
    
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        int count = 0;
        int num = scan.nextInt();
        int[] arr = new int[num];
        for (int i = 0; i < num; i++) {
            arr[count] = scan.nextInt();
            count++;
        }
        // int[] arr = {5,3,4,7,6,2,1};
        for (int a : bubleSort(arr)) {
            System.out.println(a);
        }

        scan.close();
    }
}

문제 : https://www.acmicpc.net/problem/2750


import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		int count = 0;
		int num = scan.nextInt();
		int[] arr = new int[num];
		for (int i = 0 ; i< num ; i++){
			arr[count] = scan.nextInt();
			// System.out.println(arr[count]);
			count++;
		}
		// int[] arr = {5,3,4,7,6,2,1};
		for (int i = 1; i < arr.length; i++) {
			int a = i - 1;
			int std = arr[i];
			while (a >= 0 && std < arr[a]) {
				arr[a + 1] = arr[a];
				a = a - 1;

			}
			arr[a + 1] = std;
		}

		for (int a : arr) {
			System.out.println(a);
		}
		scan.close();
	}
}

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


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[][] street = new int[n][3];
        int d[][] = new int[n][3];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 3; j++) {
                street[i][j] = scan.nextInt();
            }
        }
        // 입력부
        for (int j = 0; j < n; j++) {
            for (int i = 0; i < 3; i++) {
                if (j == 0) {
                    d[j][i] = street[j][i];
//                    System.out.print(d[j][i] + " ");
                } else {

                    d[j][i] = Math.min(d[j - 1][(i + 1) % 3], d[j - 1][(i + 2) % 3]) + street[j][i];
//                    System.out.print(d[j][i] + " ");
                }
            }
//            System.out.println();
        }

//        for (int a : d[n - 1]) {
//            System.out.println(a);
//        }
        System.out.println(Math.min(d[n-1][0],Math.min(d[n-1][1], d[n-1][2])));

        scan.close();
    }

}

문제 : https://www.acmicpc.net/problem/2748


간단하게 구현할 수 있는 피보나치 수열에 동적계획법을 적용해봤다.

배열을 만들어서 피보나치 메소드에 배열의 위치를 알려주어 한번 계산한 피보나치수는 다시 계산하지 않도록 만들었다.


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		//피보나치
		
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		long [] arr = new long [n+1];
		for( int i = 0 ; i < arr.length; i ++) {
			arr[i] = -1;
		}
		System.out.println(fi(n, arr));
//		for(int a : arr) {
//			System.out.print(a+" ");
//		}
		scan.close();
	}
	static long fi(int num,long [] arr) {
		if(arr[num] != -1) {
			return arr[num];
		}
		if (num == 0 || num == 1) {
			arr[num] = num;
			return num;
		} else {
			long result = fi(num-1,arr) + fi(num-2,arr);
			arr[num] = result;
			return result;
		}
	}

}

+ Recent posts

"여기"를 클릭하면 광고 제거.