배열을 사용하여 시험점수를 높은 순으로 정렬하기

윤서준·2021년 9월 22일
0

C++

목록 보기
2/8
#include <iostream>
using namespace std;

int main()
{
	const int num = 5; // 인원수를 const 상수에 저장해서 사용
	int test[num];
	cout << num << "명의 점수를 출력합니다." << endl;
	for (int i = 0; i < 5; i++)
	{
		cin >> test[i];
	}

	for (int s = 0; s < num - 1; s++)  
	{
		for (int t = s + 1; t < num; t++) // s와 s+1의 수를 반복해서 비교하면서 더 큰 수의 배열을 우선순위로 정렬
		{
			if (test[t] > test[s])
			{
				int tmp = test[t];
				test[t] = test[s];
				test[s] = tmp;   
			}
		}
	}

	for (int j = 0; j < num; j++)
	{
		cout << j + 1 << "번째 사람의 점수는 : " << test[j] << "입니다" << endl;
	}

	return 0;
}

0개의 댓글