Notice
Recent Posts
Recent Comments
Link
250x250
«   2025/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
Archives
Today
Total
관리 메뉴

피너클의 it공부방

백준 1205 등수 구하기 (c++) : 피너클 본문

백준

백준 1205 등수 구하기 (c++) : 피너클

피너클 2022. 5. 13. 15:02
728x90
반응형

https://www.acmicpc.net/problem/1205

 

1205번: 등수 구하기

첫째 줄에 N, 태수의 새로운 점수, 그리고 P가 주어진다. P는 10보다 크거나 같고, 50보다 작거나 같은 정수, N은 0보다 크거나 같고, P보다 작거나 같은 정수이다. 그리고 모든 점수는 2,000,000,000보

www.acmicpc.net

단순 구현 문제다.

cin >> n >> score >> p;
for (int i = 0; i < n; i++) {
	int a;
	cin >> a;
	v.push_back(a);
}

전부 입력받은 다음

if (n == p && v[v.size() - 1] >= score) cout << -1 << endl;

처음부터 랭킹 리스트가 가득 차있고 리스트 마지막 점수보다 점수가 낮다면 -1을 출력한다.

else {
	int rank = 1;
	for (int i = 0; i < n; i++) {
		if (v[i] > score) rank++;
		else break;
	}
	cout << rank << endl;
}

아니라면 변수 rank를 선언한뒤 1로 초기화한다.

리스트를 돌아보며 만약 리스트의 점수가 score보다 크다면 rank에 1을 더하고 아니면 바로 반복문을 빠져나온다.

마지막으로 rank를 출력한다.

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int n, score, p;
vector<int> v;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> score >> p;
	for (int i = 0; i < n; i++) {
		int a;
		cin >> a;
		v.push_back(a);
	}
	if (n == p && v[v.size() - 1] >= score) cout << -1 << endl;
	else {
		int rank = 1;
		for (int i = 0; i < n; i++) {
			if (v[i] > score) rank++;
			else break;
		}
		cout << rank << endl;
	}
}

전체코드다.

728x90
반응형
Comments