백준 코딩테스트 3-5-25314 코딩은 체육과목 입니다 코딩 문제 java로 푸는 방법
백준 코딩테스트 단계별로 풀어보기
3단계 반복문 5단계 25314 코딩은 체육과목 입니다 문제입니다
https://www.acmicpc.net/problem/25314
25314번: 코딩은 체육과목 입니다
오늘은 혜아의 면접 날이다. 면접 준비를 열심히 해서 앞선 질문들을 잘 대답한 혜아는 이제 마지막으로 칠판에 직접 코딩하는 문제를 받았다. 혜아가 받은 문제는 두 수를 더하는 문제였다. C++
www.acmicpc.net
단계별로 풀어보기
단계별은 @jh05013님이 관리하고 계십니다. 단계제목설명정보총 문제내가 맞은 문제1입출력과 사칙연산입력, 출력과 사칙연산을 연습해 봅시다. Hello World!132조건문if 등의 조건문을 사용해 봅시다
www.acmicpc.net
문제
오늘은 혜아의 면접 날이다.
면접 준비를 열심히 해서 앞선 질문들을 잘 대답한 혜아는 이제 마지막으로 칠판에 직접 코딩하는 문제를 받았다.
혜아가 받은 문제는 두 수를 더하는 문제였다.
C++ 책을 열심히 읽었던 혜아는 간단히 두 수를 더하는 코드를 칠판에 적었다.
코드를 본 면접관은 다음 질문을 했다.
“만약, 입출력이 N바이트 크기의 정수라면 프로그램을 어떻게 구현해야 할까요?”
혜아는 책에 있는 정수 자료형과 관련된 내용을 기억해 냈다.
책에는 long int는 4바이트 정수까지 저장할 수 있는 정수 자료형이고 long long int는 8바이트 정수까지 저장할 수 있는 정수 자료형이라고 적혀 있었다.
혜아는 이런 생각이 들었다. “int 앞에 long을 하나씩 더 붙일 때마다 4바이트씩 저장할 수 있는 공간이 늘어나는 걸까?
분명 long long long int는 12바이트, long long long long int는 16바이트까지 저장할 수 있는 정수 자료형일 거야!”
그렇게 혜아는 당황하는 면접관의 얼굴을 뒤로한 채 칠판에 정수 자료형을 써 내려가기 시작했다.
혜아가 N바이트 정수까지 저장할 수 있다고 생각해서 칠판에 쓴 정수 자료형의 이름은 무엇일까?
입력
첫 번째 줄에는 문제의 정수 N이 주어진다.
(4<=N<=1000; N은 4의 배수)
출력
혜아가 N바이트 정수까지 저장할 수 있다고 생각하는 정수 자료형의 이름을 출력하여라.
노트
출력에서 long과 long, long과 int 사이에는 공백이 하나씩 들어간다.
실제로 C++에서 각 정수 자료형이 저장할 수 있는 수의 크기는 환경에 따라 달라질 수 있다.
덧붙여, 실제로 문제 내용과 같이 long long long int와 같은 자료형을 사용한 코드를 GCC의 C++ 컴파일러를 사용해 컴파일하려고 할 경우 'long long long' is too long for GCC라는 에러 메시지와 함께 컴파일되지 않는다.
정답
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
String str = "";
sc.close();
for (int i=1; i<=N/4; i++) {
str += "long ";
}
System.out.println(str + "int");
}
}
=>
4의 배수 단위로 long의 개수를 입력하고
마지막은 int로 출력
사용자로부터 자료를 입력 받는 경우 bufferedreader를 많이 쓰는데
아직 잘 몰라서 꿋꿋하게 scanner로 풀고 있다
그래도 슬슬 bufferedreader를 쓸 때가 되지 않았나 해서 연습
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int ans = N/4;
System.out.println("long ".repeat(ans) + "int");
}
}

이 짧은 코드 내에서도 메모리와 코드 길이 차이가 나는 것을 확인할 수 있다
1-1-2557 Hello World https://developernew.tistory.com/41 <- 백준 답지 제출 방법
1-2-1000 A+B https://developernew.tistory.com/42
1-3-1001 A-B https://developernew.tistory.com/43
1-4-10998 AXB https://developernew.tistory.com/44
1-5-1008 A/B https://developernew.tistory.com/45
1-6-10869 사칙연산 https://developernew.tistory.com/46
1-7-10926 ??! https://developernew.tistory.com/47
1-8-18108 1998년생인 내가 태국에서는 2541년생?! https://developernew.tistory.com/48
1-9-10430 나머지 https://developernew.tistory.com/49
1-10-2588 곱셈 https://developernew.tistory.com/50
1-11-11382 꼬마 정민 https://developernew.tistory.com/51
1-12-10171 고양이 https://developernew.tistory.com/52
1-13-10172 개 https://developernew.tistory.com/53
2-1-1330 두 수 비교하기 https://developernew.tistory.com/54
2-2-9498 시험 성적 https://developernew.tistory.com/55
2-3-2753 윤년 https://developernew.tistory.com/56
2-4-14681 사분면 고르기 https://developernew.tistory.com/61
2-5-2884 알람 시계 https://developernew.tistory.com/68
2-6-2525 오븐 시계 https://developernew.tistory.com/69
2-7-2480 주사위 https://developernew.tistory.com/72
3-1-2739 구구단 https://developernew.tistory.com/73
3-2-10950 A+B - 3 https://developernew.tistory.com/75
3-3-8393 합 https://developernew.tistory.com/77
3-4-25304 영수증 https://developernew.tistory.com/79