-
카테고리
-
세부 분야
알고리즘 · 자료구조
-
해결 여부
미해결
간단한 질문드립니다 !
22.01.02 15:32 작성 조회수 149
0
안녕하세요 강사님, 유익한 강의 즐겨보고 있습니다!
이번 강의 관련해서 간단한 질문이 하나 있는데요,
제 아래 코드와 같이 정렬 for문안에서 3등 점수를 찾는순간 출력하고 프로그램을 종료하도록 짜면은 3등 이외에 등수들을 정렬하는 시간을 아낄 수 있지 않을까요 ?
#include <iostream>
using namespace std;
#include <vector>
int main ()
{
freopen("input.txt", "r", stdin);
int n = 0;
int temp = 0;
int indx = 0;
int order = 1;
cin >> n;
vector <int> a (n);
for (int i = 0; i < n; i++)
{
cin >> a [i];
}
for (int i = 0; i< n-1; i++)
{
temp = a[i];
indx = i;
for (int j = i+1; j < n; j++)
{
if (a[j] > a [indx])
{
indx = j;
}
}
a[i] = a[indx];
a[indx] = temp;
if (i!=0 && (a[i] != a[i-1]))
{
order ++;
if (order == 3)
{
cout << "third place is " << a[i] << endl;
return 0;
}
}
}
return 0;
}
답변을 작성해보세요.
0
답변 1