본문 바로가기

프로그래밍 언어/C, C++

[Hackerrank] 15. Classes and Objects

15. 클래스와 객체

[ 난이도: Easy | 분야: Classes ]

1. 내용 정리

클래스는 객체의 청사진을 정의한다. 우리는 다른 기본적은 자료형의 변수를 정의하듯이 객체의 클래스를 정의하는데 동일한 구문을 사용한다. 예를 들어:

Box box1;	// Declares variable box1 of type Box
Box box2;	// Declares variable box2 of type Box

 

2. 과제

문제 정의

Kristen은 그녀의 고등학교에서의 졸업생 대표 경쟁자이다.

그녀는 얼마나 많은 학생들이 이번 학기에 5개의 과목에서 그녀보다 높은 점수를 받았는지 알고 싶다.

다음의 특징을 가지는 Student라는 클래스를 만들어라:

- scores: 학생의 5개 과목의 점수를 가지고 있다.

- void input(): 5개의 정수를 읽어와서 scores에 저장한다.

- int calculateTotalScore(): 학생들의 점수의 합산을 반환한다.

입력 형식

대부분의 입력은 코드 에디터가 알아서 해줄 것이다.

void Student::input() 함수에서는, stdin으로 5개의 점수를 읽고 scores에 점수를 입력해야 한다.

제약 사항

n은 1보다 크거나 같고 100보다 작거나 같다.

examscore은 0보다 크거나 같고 50보다 작거나 같다.

출력 형식

int Student::calculateTotalScore() 함수에서, 학생의 총합 점수를 반환해야 한다(scores의 모든 값들의 합).

에디터의 잠겨있는 코드는 얼마나 많은 점수들이 Kristen의 점수보다 큰 지 판단하고 그 수를 콘솔에 출력해준다.

입력 예시

첫 번째 줄은 n을 포함하고 n은 Kristen 반 학생 수를 의미한다.

n개의 하위줄은 각 학생들의 이번 학기 5 과목 점수들을 나타낸다.

3
30 40 45 10 10
40 40 40 10 10
50 20 30 10 10

출력 예시

1

설명

Kristen의 점수는 점수 항목의 첫 번째 줄에 있다. 오직 1명의 학생의 점수가 그녀보다 높다.

문제

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cassert>
using namespace std;

// Write your Student class here

int main() {
    int n; // number of students
    cin >> n;
    Student *s = new Student[n]; // an array of n students
    
    for(int i = 0; i < n; i++){
        s[i].input();
    }

    // calculate kristen's score
    int kristen_score = s[0].calculateTotalScore();

    // determine how many students scored higher than kristen
    int count = 0; 
    for(int i = 1; i < n; i++){
        int total = s[i].calculateTotalScore();
        if(total > kristen_score){
            count++;
        }
    }

    // print result
    cout << count;
    
    return 0;
}
더보기

정답

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cassert>
using namespace std;

// Write your Student class here
class Student {
    private:
        vector<int> scores;
    public:
        void input() {
            int a, b, c, d, e;
            cin >> a >> b >> c >> d >> e;
            scores.push_back(a);
            scores.push_back(b);
            scores.push_back(c);
            scores.push_back(d);
            scores.push_back(e);
        }
        int calculateTotalScore() {
            int totalScore = 0;
            for(int i = 0; i < 5; i++) {
                totalScore += scores[i];
            }
            return totalScore;
        }
};

int main() {
    int n; // number of students
    cin >> n;
    Student *s = new Student[n]; // an array of n students
    
    for(int i = 0; i < n; i++){
        s[i].input();
    }

    // calculate kristen's score
    int kristen_score = s[0].calculateTotalScore();

    // determine how many students scored higher than kristen
    int count = 0; 
    for(int i = 1; i < n; i++){
        int total = s[i].calculateTotalScore();
        if(total > kristen_score){
            count++;
        }
    }

    // print result
    cout << count;
    
    return 0;
}

 

 

 

 

©️ Hackerrank. All Rights Reserved.

'프로그래밍 언어 > C, C++' 카테고리의 다른 글

[Hackerrank] 17. Inherited Code  (0) 2024.02.14
[Hackerrank] 16. Box It!  (0) 2024.02.13
[Hackerrank] 14. Classes  (0) 2024.02.09
[Hackerrank] 13. Structs  (0) 2024.02.09
[Hackerrank] 11. Strings  (0) 2024.02.08