03. 자료형 기본편
[ 난이도: Easy | 분야: Introduction ]
1. 자료형
C++에서의 자료형과 format specifier 그리고 비트 길이는 다음과 같다:
1) Int ("%d"): 32비트 정수
2) long ("%ld"): 64비트 정수
3) char("%c"): 문자형
4) float("%f"): 32비트 실수
5) double("%lf"): 64비트 실수
읽기
자료형을 읽기 위해서는, 아래와 같은 구문을 사용해야 한다:
scanf("'format_specifier'", %val)
예를 들어, 문자와 double 형식의 데이터를 읽기 위해서는:
char ch;
double d;
scanf("%c %lf", &ch, &d);
이런 형식으로 작성하면, format specifier의 공백을 굳이 설정할 필요는 없다.
표시하기
자료형을 표시하기 위해서는, 아래의 구문을 사용해야 한다:
printf("'format_specifier'", val)
예를 들어, 문자 다음에 double 형식의 데이터를 표시하기 위해서는:
char ch = 'd';
double d = 234.432;
printf("%c %lf", ch, d);
주의: 물론 scanf와 printf 대신하여 cin과 cout을 사용할 수 있다. 하지만, 수백만개의 입력값과 수 백만 줄을 출력해야 한다면, scanf와 printf를 사용하는 편이 훨씬 빠르다.
2. 과제
입력 형식
입력값은 다음과 같은 공백으로 구분된 값들로 구성되어 있다: int, long, char, float, 그리고 double.
출력 형식
입력값이 들어온 순서대로 각 요소들을 한줄에 하나씩 출력해야 한다. 부동소수점은 소수점 세 자리까지 표시하고, double은 소수점 아홉 자리까지 표시해야 한다.
입력 예시
3 12345678912345 a 334.23 14049.30493
출력 예시
3
12345678912345
a
334.23
14049.30493
문제
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
// Complete the code.
int a;
long b;
char c;
float d;
double e;
return 0;
}
더보기
정답
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
// Complete the code.
int a;
long b;
char c;
float d;
double e;
scanf("%d %ld %c %f %lf", &a, &b, &c, &d, &e);
printf("%d\n", a);
printf("%ld\n", b);
printf("%c\n", c);
printf("%f\n", d);
printf("%lf\n", e);
return 0;
}
ⓒ Hackerrank. All Rights Reserved.
'프로그래밍 언어 > C, C++' 카테고리의 다른 글
[Hackerrank] 06. Functions (1) | 2024.02.05 |
---|---|
[Hackerrank] 05. For Loop (1) | 2024.02.05 |
[Hackerrank] 04. Conditional Statements (1) | 2024.02.03 |
[Hackerrank] 02. Input and Output (1) | 2024.02.03 |
[Hackerrank] 01. Say "Hello, World!" With C++ (1) | 2024.02.03 |