매일 Algorithm

신재원·2023년 4월 8일
0

Algorithm

목록 보기
90/243

백준 1402번 (Silver 5)

import java.util.Scanner;

public class problem289 {
    /**
* 정답 로직은 되게 간단하다. (문제 해석이 어려울뿐)
* 예제 입력 1의 예시로 A=6, B=5에서, A = A*(-1)*(-1)*1 이므로, A-1-1+1 = B 이다.
* 어떠한 양수도 만들수 있다. (테스트 갯수대로 yes를 출력해주면된다는 뜻)
     */

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int t = in.nextInt();
        for (int i = 0; i < t; i++) {
            System.out.println("yes");
        }

    }
}

백준 1769번 (Silver 5)

import java.util.Scanner;

public class problem290 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        String n = in.next();

        int count = 0;
        while (true) {
            int sum = 0;
            // 탈출 조건
            if (n.length() == 1) {
                break;
            }

            // 자릿수 누적합
            for (int i = 0; i < n.length(); i++) {
                sum += n.charAt(i) - '0';
            }
            count++;
            // String으로 값 변환
            n = String.valueOf(sum);
        }

        if (Integer.parseInt(String.valueOf(n)) % 3 == 0) {
            System.out.println(count);
            System.out.println("YES");
        } else {
            System.out.println(count);
            System.out.println("NO");
        }


    }
}

0개의 댓글