문제

5명의 학생들 중 입력된 학번, 이름, 성적 중 학생의 성적을 기준으로 내림차순으로 구현하되

해당 학번의 학생 정보 전체가 아래와 같이 정렬되도록 순환함수를 이용해 구현하시오.

 

 

 

풀이

주의) 제 소스코드는 자료구조 교재 끝부분에 배우는 정렬 단원의 "퀵정렬"을 이용한 코드입니다. 학교진도에 맞춰서 공부하신다면 1,2,3,4,5일 때 모두 하나씩 비교해가며 5,4,3,2,1 로 만드는 방법으로 구현해보시는게 좋을 것 같습니다.

교수님께서 주신 기본 코드를 유지하면서 순환을 이용하여 해결할 수 있도록 퀵정렬로 풀었습니다.

추가점수 옵션으로 구조체 배열대신 구조체 포인터 변수로 동적할당받아서 해결하는 조건이 있어서

84행을 85행으로 고쳐서 풀었습니다. 연산이 종료되고 동적할당을 free했습니다!

 

 

코드

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#pragma warning(disable:4996)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#define MAX 5
 
typedef struct student
{
    int student_no; //학번
    char name[40]; //이름
    int score; //성적
}student;
 
//배열 교환
void SWAP(student* arr, int a, int b)
{
    //한 배열이 뭉탱이로 옮겨짐.(정보갱신)
    student temp;
    temp = arr[a];
    arr[a] = arr[b];
    arr[b] = temp;
}
// 배열, 하한, 상한)
void SORT(student* arr, int m, int n, int choice)
{
    //선택 옵션이 1이면 성적순 내림차순정렬
        if (choice == 1)
        {
            if (m < n)
            {
                int key = m;
                int i = m + 1;
                int j = n;
                //엇갈리지 않을동안
                while (i <= j)
                {
                    while (i <= n && arr[i].score >= arr[key].score)
                        i++;
                    while (j > m&& arr[j].score <= arr[key].score)
                        j--;
                    //엇갈리면 j와 key값 교체
                    if (i > j)
                        SWAP(arr, j, key);
                    //엇갈리지않으면 i와 j 교체
                    else
                        SWAP(arr, i, j);
                }
                //각각 정렬된 수의 전, 후 에서 똑같이 순환반복
                SORT(arr, m, j - 1,choice);
                SORT(arr, j + 1, n,choice);
            }
        }
        //선택 옵션이 2이면 학번순 내림차순 정렬
        else if (choice == 2)
        {
                if (m < n)
                {
                    int key = m;
                    int i = m + 1;
                    int j = n;
                    //엇갈리지 않을동안
                    while (i <= j)
                    {
                        while (i <= n && arr[i].student_no >= arr[key].student_no)
                            i++;
                        while (j > m&& arr[j].student_no <= arr[key].student_no)
                            j--;
                        //엇갈리면 j와 key값 교체
                        if (i > j)
                            SWAP(arr, j, key);
                        //엇갈리지않으면 i와 j 교체
                        else
                            SWAP(arr, i, j);
                    }
                    //각각 정렬된 수의 전, 후 에서 똑같이 순환반복
                    SORT(arr, m, j - 1, choice);
                    SORT(arr, j + 1, n, choice);
                }
        }
}
int main() {
 
    //student s[MAX];
    student* s = (student*)malloc(sizeof(student) * 5);
    int i = 0;
    //입력받을 옵션변수
    int choice;
    char str[40];
    //stduent information field
    for (i = 0; i < MAX; i++)
    {
        printf("Student Information (학번, 이름, 성적) : ");
        scanf("%d"&s[i].student_no);
        scanf("%s", str);
        strcpy(s[i].name, str);
        scanf("%d"&s[i].score);
    }
    printf("성적순 정렬은 1, 학번순 정렬은 2를입력하세요: ");
    scanf("%d"&choice);
    SORT(s, 0, MAX - 1,choice); //소트(학생 배열,0,num-1)
    for (i = 0; i < MAX; i++)
        printf("%d\t%s\t%d\n", s[i].student_no, s[i].name, s[i].score);
    printf("\n");
    //동적할당 해제
    free(s);
    return 0;
}
cs

 

+ Recent posts