250x250
Notice
Recent Posts
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
관리 메뉴

일상 코딩

[C++/inflearn C++ 코딩테스트 강의] 석차 구하기(브루트포스) 본문

코딩테스트/inflearn C++ 코딩테스트

[C++/inflearn C++ 코딩테스트 강의] 석차 구하기(브루트포스)

polarcompass 2022. 8. 27. 22:40
728x90
C++ 온라인 컴파일러 사이트
  1. https://replit.com/~
  2. https://www.onlinegdb.com/online_c++_compiler
  3. https://cpp.sh/
  4. https://www.tutorialspoint.com/compile_cpp_online.php
25. 석차 구하기
N명의 학생의 수학점수가 입력되면 각 학생의 석차를 입려된 순서대로 출력하는 프로그램을 작성하세요.
입력설명
첫 줄에 N(1<= N <= 100)이 입력되고, 두 번째 줄에 수학점수를 의미하는 N개의 정수가 입력된다.
같은 점수가 입력될 경우 높은 석차로 동일 처리한다. 
즉, 가장 높은 점수가 92점인데, 92점이 3명 존재하면 1등이 3명이고, 그 다음 학생은 4등이 된다.
점수는 100점 만점이다.
입력예제
5
90 85 92 95 90
출력예제
3 5 2 1 3

 

#include<fstream>
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>

using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	ifstream cin;
	//ofstream cout;

	cin.open("input.txt");
	int N(0); // 명수
	int a(0); // 점수
	vector< pair<int,int> > score;	  // 점수 리스트
	vector< pair< pair<int,int>, int > > rank; // 등수 리스트

	
	cin >> N;
	
	for (int i = 0; i < N; i++)
	{
		cin >> a;
		score.push_back(make_pair(i+1, a));
	}

	auto desc = [](pair<int, int> a, pair<int, int> b) { return a.second > b.second; };
	std::sort(score.begin(), score.end(), desc);

	for (auto e : score)
		cout << e.first << " " << e.second << endl;

	cout << endl;

	int pre_score = score.at(0).second;
	int pre_rank = 1;
	for (int i = 0; i < N; i++)
	{
		if (score.at(i).second == pre_score)
        	// python과 같이 튜플(입력된 순서, 점수, 점수로 정렬한 등수)로 사용하기 위해
            // C++에서 구현하려하니 이렇게 복잡해졌을뿐, python에선 간단히 해결됨
			rank.push_back(make_pair(make_pair(score.at(i).first, score.at(i).second), pre_rank));
		else
			rank.push_back(make_pair(make_pair(score.at(i).first, score.at(i).second), i+1));

		pre_score = score.at(i).second;
		pre_rank = i + 1;
	}

	for (auto e : rank)
		cout << e.first.first << " " << e.first.second << " " << e.second << endl;


	cout << endl;

	std::sort(rank.begin(), rank.end());

	for (auto e : rank)
		cout << e.second << " ";
	cout << endl;

	return 0;
}

터미널 출력 예시

인강 답안
#include<fstream>
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>

using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	ifstream cin;
	//ofstream cout;

	cin.open("input.txt");
	int N(0); // 명수
	int a(0); // 점수
	int i(0); // 1st index
	int j(0); // 2nd index

	cin >> N;

	// array 동적 배열
	int *score = new int[N+2]; // 점수 배열
	int *rank = new int[N+2]; // 등수 배열

	// rank 1로 초기화
	for ( i = 1; i <= N; i++)
	{
		rank[i] = 1;
	}
	
    // score 입력
	for (i = 1; i <= N; i++)
	{
		cin >> a;
		score[i] = a;
	}

	// 2중 for문으로 rank update
	for (i = 1; i <= N; i++)
	{
		for (j = 1; j <= N; j++)
		{
			if (score[i] < score[j])
			{
				rank[i]++;
			}
		}
	}

	for (i = 1; i <= N; i++)
	{
		cout << rank[i] << " ";
	}
	cout << endl;

	delete[] score;
	delete[] rank;

	return 0;
}
728x90