레이블이 algospot인 게시물을 표시합니다. 모든 게시물 표시
레이블이 algospot인 게시물을 표시합니다. 모든 게시물 표시

2014년 11월 3일 월요일

[AlgoSpot] DRAWRECT

  • 접근방법
    • 사각형은 각각 같은 라인에 2점씩 위치하게 됩니다.
    • 때문에 x축 좌표와, y축 좌표에 있어서 각각 2개씩 존재하게 됩니다.
    • 총 입력받은 3개의 좌표죽에 같은 x축,y축 값이 없는 좌표를 출력합니다.




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Scanner;

public class DRAWRECT {

    public static void main(String[] args) {
     Scanner sc = new Scanner(System.in);
  int cases = sc.nextInt();
  while (cases-- > 0) {
   int x[] = new int[3];
   int y[] = new int[3];
   
   x[0] = sc.nextInt();
   y[0] = sc.nextInt();

   x[1] = sc.nextInt();
   y[1] = sc.nextInt();

   x[2] = sc.nextInt();
   y[2] = sc.nextInt();

   System.out.println((x[0]==x[1]?x[2]:x[0]==x[2]?x[1]:x[0])+" "+(y[0]==y[1]?y[2]:y[0]==y[2]?y[1]:y[0]));
  }
    }
}



  • 취준생의 공부 정리 방입니다. algospot 게시물에서 문제 풀이에 대한 포스팅은 언제나 환영이라는 글을 보았기에 정리겸, 공부겸 겸사겸사 문제풀이를 올립니다. 저작권에 문제 있을시 자삭하겠습니다.

  • 엉터리일수도(아마도 대부분) 느리기도 하지만 풀었다는 것에 의의를 두고 있습니다.
  • [AlgoSpot] STRJOIN

    • 접근방법
      • 누계하는 비용을 최소화 하는 문제입니다.
      • 가장 비용이 적게 드는(크기가 작은 문자열) 2개를 합하여 새로운 비용을 등록하고 기존의 문자열크기 2개 삭제
      • 위를 반복하고 비용의 누적을 출력
    • 해결방법
      • 적은 문자열(비용)을 구하기 위하여 ArrayList를 이용하여 Sort를 사용
      • add, remove, get을 적절히 사용


     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
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Scanner;
    
    public class STRJOIN {
    
        public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
      int cases = sc.nextInt();
      while (cases-- > 0) {
       int n = sc.nextInt();
       ArrayList list = new ArrayList();
       for(int i = 0;n>i;i++)
        list.add(sc.nextInt());
       
       int tmp=0,cost=0;
       for(int i = 0;n-1>i;i++){
        Collections.sort(list);
        tmp=(int)list.get(0)+(int)list.get(1);
        cost+=tmp;
        list.add(tmp);
        list.remove(0);
        list.remove(0);
       }
       System.out.println(cost);
      }
        }
    }
    

  • 취준생의 공부 정리 방입니다. algospot 게시물에서 문제 풀이에 대한 포스팅은 언제나 환영이라는 글을 보았기에 정리겸, 공부겸 겸사겸사 문제풀이를 올립니다. 저작권에 문제 있을시 자삭하겠습니다.
  • 엉터리일수도(아마도 대부분) 느리기도 하지만 풀었다는 것에 의의를 두고 있습니다.
  • 2014년 10월 15일 수요일

    [AlgoSpot] FIX

    • Collection의 sort 메소드를 이용하여 쉽게 정렬하고 이를 이용하여 정리를 하고 비교하는 방식으로 하였습니다.


     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
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Scanner;
    
    public class FIX {
    
        public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
      int cases = sc.nextInt();
      while (cases-- > 0) {
       int times = sc.nextInt();
       ArrayList<integer> Q = new ArrayList<integer>();
       for(int i=0;times&gt;i;i++)
        Q.add(sc.nextInt());
       ArrayList<integer> copy_Q = new ArrayList<integer>(Q);
       Collections.sort(Q);
       int count = 0;
       for(int i = 0;times&gt;i;i++)
        if(Q.get(i)==copy_Q.get(i))
         count++;
       
       System.out.println(count);
      }
        }
    }
    




    • 취준생의 공부 정리 방입니다. algospot 게시물에서 문제 풀이에 대한 포스팅은 언제나 환영이라는 글을 보았기에 정리겸, 공부겸 겸사겸사 문제풀이를 올립니다. 저작권에 문제 있을시 자삭하겠습니다.
    • 엉터리일수도(아마도 대부분) 느리기도 하지만 풀었다는 것에 의의를 두고 있습니다.

    [AlgoSpot] WEEKLYCALENDAR

    • 주일달력 만들기 입니다.
    • 요일을 인식하여 인덱스로 반환하는 부분을 if문이나 삼항연산사로 실험해보았으나 해쉬맵을 이용하는 방법이 조금이나마 빨랐습니다.
    • 요일을 입력받아 요일에 해당하는 위치를 인덱스로 반환하여 for문으로 출력할때에 정확한 위치에 나오도록 하였습니다.




     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
    import java.util.HashMap;
    import java.util.Scanner;
    
    public class WEEKLYCALENDAR {
    
        public static void main(String[] args) {
         int end_day[] = {31,28,31,30,31,30,31,31,30,31,30,31};
         HashMap<string integer=""> days= new HashMap<string integer="">();
         days.put("Sunday", 1);
         days.put("Monday", 2);
         days.put("Tuesday", 3);
         days.put("Wednesday", 4);
         days.put("Thursday", 5);
         days.put("Friday", 6);
         days.put("Saturday", 7);
         Scanner sc = new Scanner(System.in);
      int cases = sc.nextInt();
      while (cases-- &gt; 0) {
       int month = sc.nextInt();
       int last_month = month==1?11:month-2;
       int day = sc.nextInt();
       String week = sc.next();
       int index = days.get(week);
       int weeks[] = new int[7];
                for(int i=1;7&gt;=i;i++){
                    int print = day-index+i&lt;1 data-blogger-escaped-day-index="" data-blogger-escaped-end_day="" data-blogger-escaped-i="" data-blogger-escaped-last_month=""&gt;end_day[month-1]?day-index+i-end_day[month-1]:day-index+i);
                    weeks[i-1]=print;
                }
       System.out.println(weeks[0]+" "+weeks[1]+" "+weeks[2]+" "+weeks[3]+" "+weeks[4]+" "+weeks[5]+" "+weeks[6]);
      }
        }
    }
    

  • 취준생의 공부 정리 방입니다. algospot 게시물에서 문제 풀이에 대한 포스팅은 언제나 환영이라는 글을 보았기에 정리겸, 공부겸 겸사겸사 문제풀이를 올립니다. 저작권에 문제 있을시 자삭하겠습니다.
  • 엉터리일수도(아마도 대부분) 느리기도 하지만 풀었다는 것에 의의를 두고 있습니다.
  • [AlgoSpot] HammingCode

    • 단순 해밍코드 구현입니다. 이문제에서는 7 4 해밍코드 구현입니다.



     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    import java.util.Scanner;
    
    public class HAMMINGCODE {
    
        public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      int cases = sc.nextInt();
      while (cases-- &gt; 0) {
       String sInput = sc.next();
       char[] cInput = sInput.toCharArray();
       int iParity = (cInput[0]^cInput[2]^cInput[4]^cInput[6])*1+(cInput[1]^cInput[2]^cInput[5]^cInput[6])*2+(cInput[3]^cInput[4]^cInput[5]^cInput[6])*4;
       if(iParity != 0){
        cInput[iParity-1] = cInput[iParity-1]=='0'?'1':'0';
       }
       char[] output = new char[]{cInput[2],cInput[4],cInput[5],cInput[6]};
       System.out.println(output);
      }
        }
    }
    

  • 취준생의 공부 정리 방입니다. algospot 게시물에서 문제 풀이에 대한 포스팅은 언제나 환영이라는 글을 보았기에 정리겸, 공부겸 겸사겸사 문제풀이를 올립니다. 저작권에 문제 있을시 자삭하겠습니다.
  • 엉터리일수도(아마도 대부분) 느리기도 하지만 풀었다는 것에 의의를 두고 있습니다.
  • 2014년 9월 29일 월요일

    [AlgoSpot] URL


    • 항상 코딩에서 문제를 발견하고 해결하는 부분이 너무나도 어이없는 부분이듯이 이 문제에서도 어이없게도 println대신 print를 써서 14번동안시도중 13번의 제출동안 해매게 되었습다.... ㅠ.ㅠ
    • 처음 문제 제출을 하였을 때 문제점은 replaceAll에 대한 메소드에 대한 것이였다. 

    public String replaceAll(String regex, String replacement)
    • replaceAll에서 regex는 정규표현식을 뜻하는 것이므로 ""으로 감싼 단순한 String으로 써서는 안된다. 때문에 정규 표현식을 사용하여 적어 넣어야 한다. 즉 "%20"이 아닌 "(%20)"이 되어야 한다.
    • replacement에서도 마찬가지 이므로 특수문자인 $을 인식시키기 위해서 \\을 사용하여야 합니다.
    • 마지막으로 저는 처음부터 찾은 함정입니다. %25 -> %으로 바꿔줘야 하는데 이 부분은 제일 마지막에 해줘야 합니다. 쉽게 '%252a'을 예로 들었을 경우 %252a -> %2a -> * 가 되기 때문입니다.


     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int cases = sc.nextInt();
            while(cases-- &gt; 0) {
                String uri = sc.next();
    
                uri = uri.replaceAll("(%20)", " ");
                uri = uri.replaceAll("(%21)", "!");
                uri = uri.replaceAll("(%24)", "\\$");
                uri = uri.replaceAll("(%28)", "(");
                uri = uri.replaceAll("(%29)", ")");
                uri = uri.replaceAll("(%2a)", "*");
                uri = uri.replaceAll("(%25)", "%");
                System.out.println(uri);
            }
        }
    }
    

    • 취준생의 공부 정리 방입니다. algospot 게시물에서 문제 풀이에 대한 포스팅은 언제나 환영이라는 글을 보았기에 정리겸, 공부겸 겸사겸사 문제풀이를 올립니다. 저작권에 문제 있을시 자삭하겠습니다.
    • 엉터리일수도(아마도 대부분) 느리기도 하지만 풀었다는 것에 의의를 두고 있습니다.