티스토리 뷰

Algorithm

[JAVA] 백준 - 입출력과 사칙연산

ellie.strong 2021. 3. 29. 21:47
728x90

백준 2557 : Hello World

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

 

백준 10718 : We love kriii

public class Main {
    public static void main(String[] args) {
        System.out.printf("강한친구 대한육군\n강한친구 대한육군");
        
        // System.out.println("강한친구 대한육군");
        // System.out.println("강한친구 대한육군");
    }
}

BufferedWriter

- newLine() : 개행 메소드

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        bw.write("강한친구 대한육군");
        bw.newLine(); // 줄 구분자
        bw.write("강한친구 대한육군");
        
        bw.flush();
        bw.close();
    }
}

StringBuilder

- 개행 메소드가 따로 없다. 

public class Main {
    public static void main(String[] args){
        StringBuilder sb = new StringBuilder();
        sb.append("강한친구 대한육군\n");
        sb.append("강한친구 대한육군");
            
        System.out.println(sb);
    }
}

 

백준 1000 : A + B

BufferedReader + StringTokenizer

- 성능면에서 StringTokenizer 사용을 권장한다. 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        String str = br.readLine();
        StringTokenizer st = new StringTokenizer(str, " ");
        // StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        
        int A = Integer.parseInt(st.nextToken());
        int B = Integer.parseInt(st.nextToken());
        
        System.out.println(A + B);
    }
}

BufferedReader + split()

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
 
public class Main {
 
	public static void main(String[] args) throws IOException {
		     
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		String[] str = br.readLine().split(" ");
		int a = Integer.parseInt(str[0]);
		int b = Integer.parseInt(str[1]);
		
		System.out.println(a+b);
 
	}
 
}
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int A = in.nextInt();
        int B = in.nextInt();
        
        System.out.println(A + B);
        
        in.close();
    }
}

 

백준 2588 : 곱셈

String.toCharArray()

- String을 char형 array로 바꿔준다. 

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

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();

        // char형의 숫자에는 '0'의 아스키코드값 만큼 빼주면 숫자가 된다.
        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));
    }
}

String.charAt(index)

- String에서 해당 index에 해당하는 char(문자) 하나를 반환해준다. 

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

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();

        System.out.println(A * (B.charAt(2) - '0'));
        System.out.println(A * (B.charAt(1) - '0'));
        System.out.println(A * (B.charAt(0) - '0'));
        System.out.println(A * Integer.parseInt(B));
    }
}
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

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);
    }
}

 

 

 

 

 

 

 

728x90
댓글
공지사항
최근에 올라온 글