[Java] SWEA 1228 암호문1

Lee GaEun·2024년 11월 14일

[Java] 알고리즘

목록 보기
17/93

1228 암호문1 문제 링크

문제분석

  • I(삽입) x, y, s
  • 앞에서부터 x의 위치 바로 다음에 y개의 숫자를 삽입
  • s는 삽입할 숫자들임

위의 규칙에 맞게 작성된 명령어를 나열하여 만든 문자열이 주어졌을 때, 암호문을 수정하고, 수정된 결과의 처음 10개 숫자를 출력하는 프로그램을 작성하여라.

제약 사항

  • 암호문은 0 ~ 999999 사이의 수로 구성

입력 조건

  • 첫째 줄 : 원본 암호문의 길이 N ( 10 ≤ N ≤ 20 의 정수)
  • 둘째 줄 : 원본 암호문
  • 셋째 줄 : 명령어의 개수 ( 5 ≤ N ≤ 10 의 정수)
  • 넷째 줄 : 명령어

출력 조건

  • #부호 + 테스트 케이스 번호 + " " + 수정된 암호문의 처음 10개 출력

#1

  • 문제 이해하는데 좀 걸림
import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileInputStream;

class Solution
{
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        /*int T;
        T=sc.nextInt();*/

        for(int test_case = 1; test_case <= 10; test_case++)
        {
            int N = sc.nextInt();
            ArrayList<Integer> code = new ArrayList<>(N);

            for(int i=0; i<N; i++) {
                code.add(sc.nextInt());
            }

            int count = sc.nextInt();

            for(int c=0; c<count; c++) {
                String a = sc.next();
                int Index = sc.nextInt();
                int N2 = sc.nextInt();

                for(int i=0; i<N2; i++) {
                    code.add(Index, sc.nextInt());
                }
            }

            System.out.print("#" + test_case);
            for(int i=0; i<10; i++) {
                System.out.print(" " + code.get(i));
            }
            System.out.println();
        }
    }
}

#2

  • code.add(Index, sc.nextInt());로 계속 같은 자리에 넣으니까 결과가 이상하지...
  • code.add(Index+i, sc.nextInt());로 수정

import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileInputStream;

class Solution
{
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        /*int T;
        T=sc.nextInt();*/

        for(int test_case = 1; test_case <= 10; test_case++)
        {
            int N = sc.nextInt();
            ArrayList<Integer> code = new ArrayList<>(N);

            for(int i=0; i<N; i++) {
                code.add(sc.nextInt());
            }

            int count = sc.nextInt();

            for(int c=0; c<count; c++) {
                String a = sc.next();
                int Index = sc.nextInt();
                int N2 = sc.nextInt();

                for(int i=0; i<N2; i++) {
                    code.add(Index+i, sc.nextInt());
                }
                for(int i=0; i<10; i++) {
                    System.out.print(code.get(i)+"\n" );
                }
                System.out.println("aaaaaaaaa");
            }

            System.out.print("#" + test_case);
            for(int i=0; i<10; i++) {
                System.out.print(" " + code.get(i));
            }
            System.out.println();
        }
    }
}

  • 성공!(25M)

5/7 다시 풀어봄

#3

15분


import java.util.*;
import java.io.FileInputStream;

class Solution
{
    static char[][] arr;
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        int T;
        T=10;

        for(int test_case = 1; test_case <= T; test_case++)
        {
            int N = sc.nextInt();
            LinkedList<Integer> arr = new LinkedList<>();
            for(int i=0; i<N; i++) {
                arr.addLast(sc.nextInt());
            }

            int comNum = sc.nextInt();
            for(int j=0; j<comNum; j++) {
                String commend = sc.next();
                int index = sc.nextInt();
                int count = sc.nextInt();
                for (int i = 0; i < count; i++) {
                    arr.add(index+i, sc.nextInt());
                }
            }

            System.out.print("#" + test_case + " ");
            for(int i=0; i<10; i++) {
                System.out.print(arr.get(i)+" ");
            }
            System.out.println();
        }
    }
}

  • 앞의 암호 10개 항만 출력하라는 글을 못 봐서 좀 걸림
  • 문제를 꼼꼼히 읽어야 되는데 시간을 생각하다보니 잘 안됨
profile
I will give it my all (๑•̀o•́๑)ง

0개의 댓글