Thursday, October 20, 2016

BFS Exmaple

import java.util.LinkedList;
import java.util.Queue;

public class BFSExample {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[][] conn = { { 0, 1, 0, 1, 0, 0, 0, 0, 1 }, // 0
                { 1, 0, 0, 0, 0, 0, 0, 1, 0 }, // 1
                { 0, 0, 0, 1, 0, 1, 0, 1, 0 }, // 2
                { 1, 0, 1, 0, 1, 0, 0, 0, 0 }, // 3
                { 0, 0, 0, 1, 0, 0, 0, 0, 1 }, // 4
                { 0, 0, 1, 0, 0, 0, 1, 0, 0 }, // 5
                { 0, 0, 0, 0, 0, 1, 0, 0, 0 }, // 6
                { 0, 1, 1, 0, 0, 0, 0, 0, 0 }, // 7
                { 1, 0, 0, 0, 1, 0, 0, 0, 0 } };// 8

        BFS(conn);
    }

    public static void BFS(int[][] conn) {
        Queue q = new LinkedList();
        
        boolean[] visited = new boolean[conn.length];
        
        for(int i = 0; i < visited.length; i++){
            visited[i] = false;
        }
        
        q.add(0);
        
        while(!q.isEmpty()){
            int nextNode;
            int i;
            nextNode= (int) q.remove();
            
            if(!visited[nextNode]){
                visited[nextNode] = true;
                System.out.println("nextNode = "+ nextNode);
                for(i = 0; i < visited.length; i++){
                    if(conn[nextNode][i] > 0 && !visited[i]){
                        q.add(i);
                    }
                }
            }
        }
        
    }
}

Sunday, October 18, 2015

주사위

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.*;

public class Main {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  Scanner sc = new Scanner(System.in);
  
  int N1 = sc.nextInt();
  int N2 = sc.nextInt();
  int N3 = sc.nextInt();
  
  int Size = N1 * N2 * N3;
  int[] answerArray = new int[Size];
  
  int answerIdx = 0;
  for(int i=1; i<=N1; i++){
   
   for(int j=1; j<=N2; j++){
    
    for(int k=1; k<=N3; k++){
     answerArray[answerIdx++] = i + j + k;
    }
   }
  }
  
  Arrays.sort(answerArray);
  
  int currentNum = answerArray[0];
  int maxNum = answerArray[0];
  int currentCnt = 1;
  int maxCnt = 1;
  
  for(int i=1; i<answerArray.length; i++){
   if(currentNum != answerArray[i]){
    if(maxCnt < currentCnt){
     maxNum = currentNum;
     maxCnt = currentCnt;
    }
    currentNum = answerArray[i];
    currentCnt = 1;
    
   }else{
    currentCnt++;
   }
   
  }
  
  System.out.println(maxNum);

 }


}

수학귀신

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.*;

public class Main {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  Scanner sc = new Scanner(System.in);
  
  int K = sc.nextInt();
  
  long[] zeroCntArray = new long[K+1];
  long[] oneCntArray = new long[K+1];
  
  int i = 0;
  zeroCntArray[i] = 0;
  oneCntArray[i++] = 0;
  
  zeroCntArray[i] = 0;
  oneCntArray[i++] = 1;
  
  if(K >= 2){
   zeroCntArray[i] = 1;
   oneCntArray[i++] = 1;
   
   for(; i<K+1; i++){
    zeroCntArray[i] = zeroCntArray[i-1] + zeroCntArray[i-2];
    oneCntArray[i] = oneCntArray[i-1] + oneCntArray[i-2];
   }
  }
  
  System.out.println(zeroCntArray[K]+" "+oneCntArray[K]);

 }


}

창고다각형

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.*;

public class Main {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  Scanner sc = new Scanner(System.in);
  
  int N = sc.nextInt();
  int[][] inputArray = new int[N][2];
  
  for(int i=0; i<N; i++){
   inputArray[i][0] = sc.nextInt();
   inputArray[i][1] = sc.nextInt();
  }
  
  sort(inputArray);
  
  int maxHeightIndex = 0;
  int maxHeight = inputArray[0][1];
  for(int i=0; i<N; i++){
   
   if(inputArray[i][1] > maxHeight){
    maxHeightIndex = i;
    maxHeight = inputArray[i][1];
   }
   
  }
  
  System.out.println(maxHeightIndex+" "+maxHeight);
  
  int currentHeight = inputArray[0][1];
  for(int i=0;i<maxHeightIndex;i++){
   
  }
  
 }
 
 
 static void sort(int[][] inputArray){
  
  for(int i=0; i<inputArray.length - 1; i++){
   
   for(int j=i; j<inputArray.length - 1; j++){
    
    if(inputArray[j][0] > inputArray[j+1][0]){
     int tempLocation = inputArray[j][1];
     inputArray[j][1] = inputArray[j+1][1];
     inputArray[j+1][1] = tempLocation;
     
     int tempHieght = inputArray[j][0];
     inputArray[j][0] = inputArray[j+1][0];
     inputArray[j+1][0] = tempHieght;
     
    }
   } 
  }

 }

}

Saturday, October 17, 2015

주차요금

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.*;

public class Main {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  Scanner sc = new Scanner(System.in);
  
  int M = sc.nextInt();
  int[] mList = new int[M]; 
  for(int i=0; i<M; i++){
   mList[i] = sc.nextInt();
  }
  
  int total = 0;
  for(int i=0; i<M; i++){
   
   int subTotal = 0;
   if(mList[i] < 10){
    subTotal = 0;
   }else if(mList[i] >= 10 && mList[i] <= 30){
    subTotal = 500;
   }else{
    subTotal = 500 + (((mList[i] - 31)/10) + 1) * 300;
   }
   
   if(subTotal > 50000){
    subTotal = 50000;
   }
   
   total += subTotal;
   
  }
  
  System.out.println(total);
  
 }

}

유의사항

1. 반복적인 System.out.print();를 이용한 출력 안됨, 한줄에 println()을 이용하여 출력할 것.

잃어버린 페이지

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.*;

public class Main {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  Scanner sc = new Scanner(System.in);
  
  int N = sc.nextInt();
  int P = sc.nextInt();
  
  int pageArrayRow = N/4;
  int[][] pageArray = new int[pageArrayRow][4];
  
  int number = 1;
  for(int i=0; i<pageArrayRow; i++){
   pageArray[i][0] = number++;
   pageArray[i][1] = number++;
  }
  
  for(int i=N/4-1; i>=0; i--){
   pageArray[i][2] = number++;
   pageArray[i][3] = number++;
  }
 
  int index = 0;
  if(P > N/2){
   if(P%2 == 0){
    index = Math.abs((P-N/2)/2 - (N/4));
   }else{
    index = Math.abs((P-N/2)/2 - (N/4-1));
   }
   
  }else{
   index = (P-1)/2;
  }
  
  int[] answer = new int[3];
  int answerIndex = 0;
  for(int i=0; i<4; i++){
   if(pageArray[index][i] != P){
     answer[answerIndex++]= pageArray[index][i];
   }
  }
  
  System.out.println(answer[0]+" "+answer[1]+" "+answer[2]);
  
  
 }

}