Programming Language/문제

[코드업] 3015 : 성적표 출력

장영현 2023. 6. 16. 20:51
728x90

https://codeup.kr/problem.php?id=3015&rid=0 

 

성적표 출력

첫째 줄에 데이터의 개수 $n$ ($3 <= n <= 100$)과 출력인원 $m$ ($1 <= m <= n$)이 공백으로 구분되어 입력된다. 둘째 줄부터 학생 이름과 점수($0~100$)가 공백으로 구분되어 입력된다.  단 이름의 길이는

codeup.kr

 

#include <stdio.h>
#include <stdlib.h>

typedef struct student {
    int score;
    int num;
    char name[10];

} student;

int main() {
    int n, m;

    scanf("%d %d", &n,&m);

    student* test = (student*)malloc(n * sizeof(student));

    for (int i = 0; i < n; i++) {
        scanf("%s %d", &(test[i].name), &(test[i].score));
        test[i].num = i;
    }

    student temp;
    for (int i = 0; i < n-1; i++) {
        for (int j = i; j < n; j++) {
            if (test[i].score < test[j].score) {
                temp = test[j];
                test[j] = test[i];
                test[i] = temp;
            }

            if ((test[i].score == test[j].score) && (test[i].num > test[j].num)) {
                temp = test[j];
                test[j] = test[i];
                test[i] = temp;
            }
        }
    }
    for (int i = 0; i < m; i++) {
        printf("%s\n", test[i].name);
    }
    free(test);

    return 0;
}

 


구조체 연습