[백준/Java] 2588 : 곱셈

SEOP·2023년 4월 28일
0

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int a = Integer.parseInt( br.readLine());
        String b = br.readLine();

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        bw.write(String.valueOf(a * (b.charAt(2) -'0')));
        bw.newLine();
        bw.write(String.valueOf(a * (b.charAt(1) -'0')));
        bw.newLine();
        bw.write(String.valueOf(a * (b.charAt(0) -'0')));
        bw.newLine();
        bw.write(String.valueOf(a*Integer.parseInt(b)));
        bw.flush();
        bw.close();
    }
}

OR

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int a = Integer.parseInt( br.readLine());
        String B = br.readLine();
        char[] b = B.toCharArray();

        System.out.println(a * (b[2]-'0'));
        System.out.println(a * (b[1]-'0'));
        System.out.println(a * (b[0]-'0'));
        System.out.println(a * Integer.parseInt(B));
    }
}

OR

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int a = Integer.parseInt( br.readLine());
        int b = Integer.parseInt( br.readLine());

        System.out.println(a * (b%10));
        System.out.println(a * (b%100/10));
        System.out.println(a * (b/100));
        System.out.println(a * b);
    }
}

charAt(String[{indexNum}] - '0')

-'0'하는 이유?
: 반환값이 아스키코드인데, 우리가 원하는 것은 아스키코드가 아닌 우리가 보는 숫자 그대로 보기 위해서

String.toCharArray()

String을 character 배열에 담는다.

profile
응애 나 애기 개발자

0개의 댓글