728x90
반응형
* 공부 목표
입출력 - 2557, 1000, 2558, 10950, 10951, 10952, 10953, 11021, 11022, 11718, 11719, 11720, 11721, 2741, 2742, 2739, 1924, 8393, 10818, 2438, 2439, 2440, 2441, 2442, 2445, 2522, 2446, 10991, 10992
- 2557번
문) Hello World!를 출력하시오.
public class Main{
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
- 1000번
문) 두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
출) 첫째 줄에 A+B를 출력한다.
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a+b);
}
}
- 2558번
문) 두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a+b);
}
}
- 10950번
문) 두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
입) 첫째 줄에 테스트 케이스의 개수 T가 주어진다.
각 테스트 케이스는 한 줄로 이루어져 있으며, 각 줄에 A와 B가 주어진다. (0 < A, B < 10)
이 문제는 2가지 방법으로 풀었음.
1) 정수 입력받고 반복문. //메모리 가장 많이 사용, 소요 시간도 가장 오래 걸림.
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int i=0; i< T; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a+b);
}
}
}
2) 배열에 넣어서 출력.
2-1) scanner 종료. //메모리 가장 적게 사용, 소요 시간도 최소.
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
int arr[] = new int[T];
for(int i=0; i< T; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
arr[i] = a+b;
}
sc.close();
for(int i=0; i<T; i++) {
System.out.println(arr[i]);
}
}
}
2-2) scanner 미종료. //메모리도 시간 소요도 2번째로 많았음(의외인 건, 1이랑 크게 차이 안났음).
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
int arr[] = new int[T];
for(int i=0; i< T; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
arr[i] = a+b;
}
for(int i=0; i<T; i++) {
System.out.println(arr[i]);
}
}
}
>> 한 가지 느낀 점, scanner 종료 여부에 따라 효율이 달라지기도 한다.
'알고리즘' 카테고리의 다른 글
[백준] 입출력 - 11721번, 열 개씩 끊어 출력. charAt(); (1) | 2023.01.20 |
---|---|
[백준] 입출력 - 11720번(서칭), 숫자의 합. (0) | 2023.01.18 |
[백준] 입출력 - 11719. 그대로 입력. (0) | 2023.01.17 |
[백준] 입출력 - 11021, 11022, 11718(서칭). (1) | 2023.01.17 |
[백준] 입출력 - 10951, 10952, 10953(모두 서칭). A+B 응용. (0) | 2023.01.15 |