본문 바로가기

연습문제

(66)
2331-반복수열 2331번: 반복수열 첫째 줄에 반복되는 부분을 제외했을 때, 수열에 남게 되는 수들의 개수를 출력한다. www.acmicpc.net 재귀함수를 사용하여 DFS를 구현하였다 1. dfs에서 숫자를 받아서 배열 저장 후 다음 순서 숫자를 구한다 2. 만약 현재 숫자와 다음숫자가 (같은순서로) 이미 배열에 존재하는 숫자의 순서라면 3. dfs를 빠져나간다 package com.exam; import java.util.ArrayList; import java.util.Scanner; public class B2331 { static int A,P; static ArrayList sumArr = new ArrayList(); static boolean finish; static void dfs(int a){ s..
10451-순열사이클 10451번: 순열 사이클 1부터 N까지 정수 N개로 이루어진 순열을 나타내는 방법은 여러 가지가 있다. 예를 들어, 8개의 수로 이루어진 순열 (3, 2, 7, 8, 1, 4, 5, 6)을 배열을 이용해 표현하면 \(\begin{pmatrix} 1 & 2 &3&4&5&6&7&8 \\ 3 www.acmicpc.net 처음에는 입력을 두 개씩 받아서 [(1,3),(2,2)....] 식으로 풀다가 index랑 비교하여 푸는 방법을 찾아서 새로 풀어보았다 import java.util.Scanner; public class B10451 { static int[] intArr; static boolean[] visited; static int dfs(int j){ if(j == intArr[j] || visit..
2178-미로탐색 2178번: 미로 탐색 첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다. www.acmicpc.net import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class B2178 { static int N,M; static int[][] Board; static int[][] D = {{-1,0},{1,0},{0,-1},{0,1}}; //상 하 좌 우 static boolean[][] visited; static class Node{ int row, col, count; Node(int r, in..
1620-DFS와 BFS 1260번: DFS와 BFS 첫째 줄에 정점의 개수 N(1 ≤ N ≤ 1,000), 간선의 개수 M(1 ≤ M ≤ 10,000), 탐색을 시작할 정점의 번호 V가 주어진다. 다음 M개의 줄에는 간선이 연결하는 두 정점의 번호가 주어진다. 어떤 두 정점 사 www.acmicpc.net package com.exam; import java.util.*; public class B1260 { static int N,L,S; static int[][] Board; static boolean[] visited; static ArrayList compare; public static void bfs(){ Queue queue = new LinkedList(); visited = new boolean[N]; visi..
16234-인구이동 package com.exam; import java.util.*; public class B16234 { static final int MAX_N = 10; static int n,l,r, result; static int[][] Board = new int[MAX_N][MAX_N]; static int[][] D = {{-1,0},{1,0},{0,-1},{0,1}}; static ArrayList union; static Queue queue; static boolean[][] visited; static class Node{ int col,row=0; Node(int row, int col){ this.row=row; this.col=col; } } public static int move(){ w..
16234-인구이동 (오답) 16234-인구이동 문제 package com.exam; import java.util.*; public class BAEK16234 { static int[][] intArr; static int n, firstNum , secondNum; static int result; static Map numMap; public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); firstNum = sc.nextInt(); secondNum = sc.nextInt(); intArr = new int[n][n]; for (int y = 0; y < n; y++) { for (int x = 0; x < n; ..
Java- Example6번 ( p506~507 ) //p506~ 507 public class Main { public static void main(String[] args) { // FileExample7 : 506p 507p 'Buffer' FileWriter fw = null; BufferedWriter bw = null; try { fw = new FileWriter("./FileExample7/bufferedWriter.txt"); bw = new BufferedWriter(fw, 10); //sz= size bw.write('A'); // < 하나의 데이터 byte bw.write('B'); bw.write('C'); bw.write('D'); Scanner sc = new Scanner(System.in); String input = nu..
Java- FileExample5번 (p490 ~ 491) //p490 ~ 491 public class Main { //DataInputStream, DataOutputStream 테스트 public static void main(String[] args) { FileOutputStream fo = null; FileInputStream fi = null; DataOutputStream dos = null; DataInputStream dis = null; try{ fo= new FileOutputStream(".\\FileExample8\\demo\\dataFile.txt"); //파일 만들기 //파일이 가지고있는 컨텐츠 : 글, 사진, 영상... 이 컨텐츠들은 byte[] 배열의 모음이다. dos= new DataOutputStream(fo); // jav..