백준

박경희·2024년 1월 17일

코딩테스트

목록 보기
23/69


2439

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i -1; j++) {
                System.out.print(" ");
            }

            for (int j = 0; j < i + 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
  • 처음에는 for문 안에 for문 안에 또 for문으로 해야하나 싶었는데 병렬로 for문을 해야 했다.

10952

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);

        int T = sc.nextInt();
        int c = 0;

        for (int i = 0; i < T ; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            System.out.println(a + b);
        }
        System.out.println(c + " " + c);
    }
}

오류

  • 앞서 풀었던 문제와 같이 입력 받는 수가 있다고 생각했다.
  • 처음에는 0 0 이 나와도 답이 없이 끝내란느거라 생각해서 풀었으나 오답.
  • 마지막 출력에 0 0 이 나오게 하는건가 싶어 적용해 보았으나 오답이었다.

찾아보니 횟수를 입력받는게 아니라
입력은 여러 개의 테스트 케이스로 이루어져 있다.라고 되어있다. 좀 말이 애매한거같지만..

그래서 여기서는 for문이 아닌 while을 사용해서 풀어야 한다.

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);

        while (true) {
            int a = sc.nextInt();
            int b = sc.nextInt();

            if (a == 0 && b == 0) {
                break;
            }
            System.out.println(a + b);
        }
    }
}

10951

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);

        while (sc.hasNextInt()) {
            int a = sc.nextInt();
            int b = sc.nextInt();

            System.out.println(a + b);
        }

        sc.close();
    }
}

hasNextInt()를 사용
입력값이 정수일 경우 true 를 반환하며, 정수가 아닐 경우 바로 예외를 던져더 이상의 입력을 받지 않고 (hasNextInt()에서) false를 반환하여 반복문이 종료되게 했다.


배열

10807

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);

         // 첫 번째 입력: 정수의 개수 N
        int N = sc.nextInt();

        // 두 번째 입력: N개의 정수 배열
        int[] v = new int[N];
        
        // 세 번째 입력: 찾으려는 정수
        for (int i = 0; i < N; i++) {
            v[i] = sc.nextInt();
        }
        int findNum = sc.nextInt();

		//개수 카운트
        int count = 0;
        for (int i = 0; i < N; i++) {
            if (v[i] == findNum) {
                count ++;
            }
        }
        System.out.println(count);

    }
}

10871

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int X = sc.nextInt();

        int[] A = new int[N];
        for (int i = 0; i < N; i++) {
            A[i] = sc.nextInt();
        }

        for (int i = 0; i < N; i++) {
            if (A[i] < X) {
                System.out.print(A[i] + " ");
            }
        }
    }
}
  • 처음에는 또 다른 배열을 만들어 담아야 하나 고민했는데 너무 복잡해지는 것 같아 다른 방법을 찾아보았다.
  • A[I]와 X를 비교해본 후 작으면 바로 출력하도록 했다.

0개의 댓글