13. 구조체
[ 난이도: Easy | 분야: Classes ]
1. 내용 정리
구조체는 복합적인 자료 구조를 표현하기 위해 여러 필드들을 혼합하는 방법 중 하나이고, 객체 지향 프로그램에서 기반한 것이다.
예를 들어, 우리는 구조체로 학생의 정보인 나이(정수), 이름(문자열), 성씨(문자열) 그리고 standard(정수)를 저장할 수 있다.
구조체는 다음과 같이 표현할 수 있다:
struct NewType {
type1 value1;
type2 value2;
.
.
.
typeN valueN;
};
2. 과제
우리는 학생 이름, 위에서 말한 데이터들을 저장할 구조체를 만들어야 한다.
입력 형식
입력은 네 줄로 구성되어 있다.
첫 번째 줄은 나이 정보를 나타내는 정수를 저장하고 있다.
두 번째 줄은 학생의 이름을 나타내는 소문자 알파벳으로 이루어진 문자열을 저장하고 있다.
세 번째 줄은 학생의 성씨를 나타내는 소문자 알파벳으로 이루어진 문자열을 저장하고 있다.
네 번째 줄은 학생의 standard를 나타내는 정수를 저장하고 있다.
주의: 이름과 성씨는 50자를 넘지 않는다.
출력 형식
출력은 단일 줄로 구성되어 있고, 나이, 이름, 성씨 그리고 standard를 표현하고 있다.
각 정보들은 공백으로 구분되어 있다.
추신: 입출력은 HackerRank에 의해 이루어집니다.
입력 예시
15
john
carmack
10
출력 예시
15 john carmack 10
문제
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
/*
add code for struct here.
*/
int main() {
Student st;
cin >> st.age >> st.first_name >> st.last_name >> st.standard;
cout << st.age << " " << st.first_name << " " << st.last_name << " " << st.standard;
return 0;
}
더보기
정답
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
/*
add code for struct here.
*/
struct Student {
int age;
string first_name;
string last_name;
int standard;
};
int main() {
Student st;
cin >> st.age >> st.first_name >> st.last_name >> st.standard;
cout << st.age << " " << st.first_name << " " << st.last_name << " " << st.standard;
return 0;
}
©️Hackerrank. All Rights Reserved.
'프로그래밍 언어 > C, C++' 카테고리의 다른 글
[Hackerrank] 15. Classes and Objects (2) | 2024.02.09 |
---|---|
[Hackerrank] 14. Classes (0) | 2024.02.09 |
[Hackerrank] 11. Strings (0) | 2024.02.08 |
[Hackerrank] 10. StringStream (0) | 2024.02.08 |
[Hackerrank] 09. Variable Sized Array (2) | 2024.02.08 |