2739번: 구구단
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
for (int i = 1; i < 10; i++) {
System.out.println(num + " * " + i + " = " + num * i);
};
}
}
10950번: A+B-3
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
for (int i = 0; i < num ; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a + b);
}
}
}
8393번: 합
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
for (int i = 0; i <= n ; i++) {
sum = sum + i;
if (i == n) {
System.out.println(sum);
}
}
}
}
15552번: 빠른 A+B (->공부 필요!)
본격적으로 for문 문제를 풀기 전에 주의해야 할 점이 있다. 입출력 방식이 느리면 여러 줄을 입력받거나 출력할 때 시간초과가 날 수 있다는 점이다.
C++을 사용하고 있고 cin/cout을 사용하고자 한다면, cin.tie(NULL)과 sync_with_stdio(false)를 둘 다 적용해 주고, endl 대신 개행문자(\n)를 쓰자. 단, 이렇게 하면 더 이상 scanf/printf/puts/getchar/putchar 등 C의 입출력 방식을 사용하면 안 된다.
Java를 사용하고 있다면, Scanner와 System.out.println 대신 BufferedReader와 BufferedWriter를 사용할 수 있다. BufferedWriter.flush는 맨 마지막에 한 번만 하면 된다.
Python을 사용하고 있다면, input 대신 sys.stdin.readline을 사용할 수 있다. 단, 이때는 맨 끝의 개행문자까지 같이 입력받기 때문에 문자열을 저장하고 싶을 경우 .rstrip()을 추가로 해 주는 것이 좋다.
또한 입력과 출력 스트림은 별개이므로, 테스트케이스를 전부 입력받아서 저장한 뒤 전부 출력할 필요는 없다. 테스트케이스를 하나 받은 뒤 하나 출력해도 된다.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine()); // 모든 입력은 BufferedReader.readLine() 으로 받았다.
StringTokenizer st;
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine(), "");
// 테스트 케이스에서 주어지는 두 정수는 문자열 분리를 위해 매 반복마다 StringTokenizer을 생성함과 동시에 문자를 입력 받는다.
bw.write((Integer.parseInt(st.nextToken()) + Integer.parseInt(st.nextToken())) + "\n");
// 각 StringTokenizer의 토큰(분리되어있는 문자)을 반환하며, 반환되는 타입은 String이므로 Integer.parseInt를 통해 int형으로 바꾸어 준다.
// 이렇게 바꾼 두 토큰을 더해준 값을 BufferedWriter.write()에 넣어준다.
}
br.close();
bw.flush(); // 그리고 거의 필수적으로 버퍼를 비운뒤(flush)
bw.close(); // 닫아줘야한다(close).
}
}
2741번: N찍기
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
System.out.println(i);
}
}
}
2742번: 기찍N
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
System.out.println((n + 1) - i);
}
}
}
11021번: A+B-7
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i <= n ; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println("Case #" + i + ": " + (a + b));
}
}
}
11022번: A+B-8
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int i = 1; i <= T; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println("Case #" + i + ": " + a + " + " + b + " = " + (a + b));
}
}
}
2438번: 별 찍기-1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
sc.close();
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
2439번: 별 찍기-2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
sc.close();
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= (N - i); j++) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
10871번: X보다 작은 수
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int X = sc.nextInt();
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= N; i++) {
int A = sc.nextInt();
if (A < X) {
sb.append(A + " ");
}
}
System.out.print(sb);
}
}
'~2023.02 > 백준 문제풀이' 카테고리의 다른 글
[JAVA] 백준 단계별로 풀어보기_5단계(1차원 배열) (0) | 2022.02.11 |
---|---|
[JAVA] 백준 단계별로 풀어보기_4단계(while문) (0) | 2022.02.08 |
[JAVA] 백준 단계별로 풀어보기_3단계(for문) (0) | 2022.02.08 |
[JAVA] 백준 단계별로 풀어보기_2단계(if문) (0) | 2022.01.30 |
[JAVA] 백준 단계별로 풀어보기_1단계(입출력과 사칙연산) (0) | 2022.01.26 |