Tuesday, September 15, 2015

참외밭 Wrong Answer

 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.io.IOException;

import java.util.Scanner;

public class Main
{

 public static void main(String[] arg) throws IOException
 {

  int[][] a = new int[6][2];

  int K, sum = 0;

  Scanner sc = new Scanner(System.in);

  // 입력받는부분

  K = sc.nextInt();

  for (int i = 0; i < 6; i++) {

   a[i][0] = sc.nextInt();
   a[i][1] = sc.nextInt();

  }
  // 여기서부터작성

  
  int[] eastWest  = new int[3];
  int[] southNorth  = new int[3];

  int eWIdx = 0;
  int sNIdx = 0;
  for (int i = 0; i < 6; i++) {
   if(a[i][0] == 1 || a[i][0] == 2){
    eastWest[eWIdx++] = a[i][1];
   }
   if(a[i][0] == 3 || a[i][0] == 4){
    southNorth[sNIdx++] = a[i][1];
   }
  }
  
  int ewBig = 0;
  int ewBigIdx = 0;
  int ewSmallIdx = 0;
  for(int i = 0;i < eastWest.length; i++){
   if(ewBig < eastWest[i]){
    ewBig = eastWest[i];
    ewBigIdx = i;
   }
  }
  
  if(ewBigIdx == 0){
   ewSmallIdx = 1;
  }else if(ewBigIdx == 1){
   ewSmallIdx = 2;
  }else if(ewBigIdx == 2){
   ewSmallIdx = 0;
  }
  
  int ewBigValue = eastWest[ewBigIdx];
  int ewSmallValue = eastWest[ewSmallIdx];
  
//////////////////////////////////////////////////////////////  
  
  int snBig = 0;
  int snBigIdx = 0;
  int snSmallIdx = 0;
  for(int i = 0;i < southNorth.length; i++){
   if(snBig < southNorth[i]){
    snBig = southNorth[i];
    snBigIdx = i;
   }
  }
  
  if(snBigIdx == 0){
   snSmallIdx = 2;
  }else if(snBigIdx == 1){
   snSmallIdx = 0;
  }else if(snBigIdx == 2){
   snSmallIdx = 1;
  }
  
  int snBigValue = southNorth[snBigIdx];
  int snSmallValue = southNorth[snSmallIdx];
  
//  System.out.println(ewBigValue+" "+ewSmallValue);
//  System.out.println(snBigValue+" "+snSmallValue);
  
  sum = ((ewBigValue * snBigValue) - (ewSmallValue * snSmallValue)) * K; 
  // 출력하는부분
  System.out.println(sum);

  sc.close();

 }

}

Rotating Array by a given angle(90, 180, 270, 360 degree)

  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
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
import java.io.IOException;

import java.util.Scanner;

public class Main
{

 public static void main(String[] arg) throws IOException
 {

  int n;

  int[][] a;

  int r;

  Scanner sc = new Scanner(System.in);

  // 입력받는부분

  n = sc.nextInt();

  a = new int[n][n];

  for (int i = 0; i < n; i++)
  {
   for (int j = 0; j < n; j++)
    a[i][j] = sc.nextInt();
  }

  for (;;)
  {
   r = sc.nextInt();

   if (r == 0)
    break;

   // 여기서부터작성
   int[][] tempA = new int[n][n];
   
   int rowXDefault = 0;
   int rowYDefault = 0;
   int rowXIncrement = 0;
   int rowYIncrement = 0;
   int colXIncrement = 0;
   int colYIncrement = 0;
   
   if(r == 90){
    rowXDefault = 0;
    rowYDefault = n -1;
    rowXIncrement = -1;
    rowYIncrement = -1;
    colXIncrement = +1;
    colYIncrement = -1;
   }else if(r == 180){
    rowXDefault = n- 1;
    rowYDefault = n -1;
    rowXIncrement = -2;
    rowYIncrement = 0;
    colXIncrement = 0;
    colYIncrement = -2;
   }else if(r == 270){
    rowXDefault = n - 1;
    rowYDefault = 0;
    rowXIncrement = -1;
    rowYIncrement = +1;
    colXIncrement = -1;
    colYIncrement = -1;
   }else if(r == 360){
    //leave them
   }
   
   
   for (int i = 0; i < n; i++)
   {
    int indexX = rowXDefault;
    int indexY = rowYDefault;
    
    for (int j = 0; j < n; j++)
    {
     tempA[i + indexX][j + indexY] = a[i][j];
     
     indexX += colXIncrement;
     indexY += colYIncrement;
    
     
    }
    rowXDefault += rowXIncrement;
    rowYDefault += rowYIncrement;
   }
   
   a = tempA;
   
   // 출력하는부분
   for (int i = 0; i < n; i++)
   {
    for (int j = 0; j < n; j++)
    {
     System.out.print(a[i][j] + " ");

    }
    System.out.println();

   }
   
  }
  sc.close();
 }
 
 
}
N*N 배열에서 각 요소에 정수가 들어 있다. 이 배열에서 합이 가장 큰 행과 열의 번호를 각각 인쇄하라.
입력예시
5
3 -5 12 3 -21
-2 11 2 -7 -11
21 -21 -35 -93 -11
9 14 39 -98 -1
-2 -2 -2 -2 -2
출력예시
2 1
 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.io.IOException;

import java.util.Scanner;

public class Main

{

 public static void main(String[] arg) throws IOException

 {

  int i, j, N;


  int[][] a;

  Scanner sc = new Scanner(System.in);

  // 입력 받는 부분

  N = sc.nextInt();

  a = new int[N][N];

  for (i = 0; i < N; i++) {

   for (j = 0; j < N; j++)
    a[i][j] = sc.nextInt();

  }

  // 여기서부터 작성
  int[] row_sum = new int[N];
  int[] col_sum = new int[N];
  
  for(i = 0; i < N; i++){
   
   int row_sum_temp = 0;
   int col_sum_temp = 0;
   
   for (j = 0; j < N; j++){
    row_sum_temp += a[i][j];
    col_sum_temp += a[j][i];
   }
   row_sum[i] = row_sum_temp; 
   col_sum[i] = col_sum_temp;
  }
  
  int max_row_index = 1;
  int max_col_index = 1;
  
  int row_max = row_sum[0];
  int col_max = col_sum[0];
  
  
  for(i = 1; i < N; i++){
   if(row_max < row_sum[i]){
    max_row_index = i + 1;
    row_max = row_sum[i];
   }

  }
  
  for(i = 1; i < N; i++){
   if(col_max < col_sum[i]){
    max_col_index = i + 1;
    col_max = col_sum[i];
//    System.out.println("max_col_index : "+max_col_index);
//    System.out.println("col_max : "+col_max);
   }
  }
  // 출력 하는 부분

  System.out.print(max_row_index + " " + max_col_index);

  sc.close();

 }

}

Computing height as stacking bowl using parenthesis, JungOl Question 2604, (http://www.jungol.co.kr/problem.php?id=2604)

 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
import java.util.*;
public class Main {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        
        String str = sc.nextLine();
        
        char[] strArray = str.toCharArray();
        
        int height = 0;
        
        for(int i = 0; i < strArray.length;i++){
         
         if(i == 0){
          height += 10;
         }else if(strArray[i] == '('){
          
          if(strArray[i-1] == '('){
           height += 5;
          }else{
           height += 10;
          }
          
         }else if(strArray[i] == ')'){
          if(strArray[i-1] == ')'){
           height += 5;
          }else{
           height += 10;
          }
         }
        }
        
        System.out.println(height);
 }
 
}

Monday, September 14, 2015

Computing the GCD(Greatest Common Divisor) and the LCM(Lowest Common Multiple) from multiple input numbers using Euclidean algorithm, JungOl Question 1002 (http://jungol.co.kr/problem.php?id=1002)

 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
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;
        N = sc.nextInt();
        int[] input_arr = new int[N];
        
        for(int i = 0;i < N; i++){
         input_arr[i] = sc.nextInt();
        }
        
        int current_gcd = input_arr[0];
        for(int i = 0;i < N-1;i++){
         if(current_gcd > input_arr[i+1]){
          current_gcd = gcd(input_arr[i+1], current_gcd % input_arr[i+1]);
         }else{
          current_gcd = gcd(current_gcd, input_arr[i+1] % current_gcd);
         }
        }
        
        
        int current_lcm = input_arr[0];
        for(int i = 0;i < N-1;i++){
         if(current_lcm > input_arr[i+1]){
          current_lcm = lcm(current_lcm, input_arr[i+1], gcd(current_lcm, input_arr[i+1]));
         }else{
          current_lcm = lcm(current_lcm, input_arr[i+1], gcd(input_arr[i+1], current_lcm));
         }
        }
        
        System.out.println(current_gcd+" "+current_lcm);

 }
 
 static int gcd(int a, int b){
  if(b == 0){
   return a;
  }
  return gcd(b, a%b);
 }
 
 static int lcm(int a, int b, int gcd)
 {
  return a*b/gcd;
 }

}

Wednesday, September 9, 2015

JungOl Question 1001 (http://jungol.co.kr/problem.php?id=1001)

 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);
        
        for(;;){
         
         //input, two numbers
         int total, totalLeg;
            total = sc.nextInt();
            totalLeg = sc.nextInt();
            
            //input 0 -> stop program
            if(total == 0 || totalLeg == 0){
             break;
            }
            
            //input validation
            if(total > 1000 || total < 0 || totalLeg > 4000 || totalLeg < 0){
             System.out.println("INPUT ERROR!");
            }else{
             
             Boolean findFlag = false; 
                
             //Generate two numbers(dog and chick), sum of which are totalLeg(second input)
                for(int dog = 0;dog <= total;dog++){
                 
                 int chick = total  - dog;
                 int computedLeg = dog * 4 + chick * 2; 
                 
                 //if the number is found -> print "dog" and "leg" and break;
                 if(totalLeg == computedLeg){
                  System.out.println(dog + " " + chick);
                  findFlag = true;
                  break;
                 }
                }
                
                //No matching -> print "0"
                if(!findFlag){
                 System.out.println("0");
                }
                
            }
            
        }

 }

}

Basic Java Example, Standard I/O, Get get numbers using standard input and display sum of two numbers

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import java.util.*;
public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int a, b;
        a = sc.nextInt();
        b = sc.nextInt();
        System.out.println(a + b);
    }
}