티스토리 뷰
💻C 코드 바로보기
문제 설명
학생들의 이름과 시험 점수가 주어질 때, K 등을 한 학생의 이름을 찾으려 합니다(단, 시험 점수가 같은 학생은 없다고 가정합니다). 이를 위해 다음과 같이 구조체를 정의했습니다.
typedef struct Student{
char name[20];
int score;
}Student;
또한, 다음과 같이 프로그램 구조를 작성했습니다.
1. 학생의 이름과 시험 점수로 구성된 구조체 배열을 생성 및 초기화합니다.
2. 생성된 구조체 배열을 시험 점수의 내림차순으로 정렬합니다.
3. 정렬된 구조체 배열에서 K - 1번째에 있는 학생의 이름을 return 합니다.
학생들의 이름이 순서대로 들어있는 배열 names와 names의 길이 names_len, 학생들의 시험 점수가 순서대로 들어있는 배열 scores와 scores의 길이 scores_len, 등수를 나타내는 자연수 K가 매개변수로 주어질 때, K 등인 학생의 이름을 return 하도록 solution 함수를 작성하려 합니다. 위 구조를 참고하여 코드가 올바르게 동작할 수 있도록 빈칸을 채워 전체 코드를 완성해주세요.
매개변수 설명
학생들의 이름이 순서대로 들어있는 배열 names와 names의 길이 names_len, 학생들의 시험 점수가 순서대로 들어있는 배열 scores와 scores의 길이 scores_len, 등수를 나타내는 자연수 K가 solution 함수의 매개변수로 주어집니다.
- names는 학생들의 이름이 들어있는 배열입니다.
- 모든 학생의 이름은 알파벳 소문자로만 이루어져 있고, 길이는 2 이상 15 이하입니다.
- 이름이 같은 학생은 없습니다.
- names_len은 5 이상 100 이하의 자연수입니다.
- scores는 학생들의 시험 점수가 들어있는 배열입니다.
- 시험 점수는 0 이상 100 이하의 정수이며, 동점자는 없습니다.
- scores_len은 5 이상 100 이하의 자연수입니다.
- names_len과 scores_len은 항상 같으며, i 번째 학생의 이름과 성적은 names[i]와 scores[i]에 들어있습니다.
- K는 등수를 나타내며, 1 이상 전체 학생의 수 이하의 자연수입니다.
return 값 설명
K 등인 학생의 이름을 return 해주세요.
예시
names | names_len | scores | scores_len | K | return |
---|---|---|---|---|---|
["lukas", "james", "levi", "eli", "max"] | 5 | [30,20,50,40,10] | 5 | 2 | "eli" |
예시 설명
각 학생의 이름을 시험 점수에 따라 내림차순으로 정렬하면 다음과 같습니다.
이름 | "levi" | "eli" | "lukas" | "james" | "max" |
시험 점수 | 50 | 40 | 30 | 20 | 10 |
K = 2 이므로, 2등은 "eli"입니다.
주어진 코드
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
typedef struct Student{
char name[20];
int score;
}Student;
int compare(const void *a, const void * b){
Student first = *(Student *)a;
Student second = *(Student *)b;
if(@@@)
return -1;
else if(@@@)
return 1;
else
return 0;
}
char* solution(char* names[], int names_len, int scores[], int scores_len, int K) {
int len = names_len;
Student* students = (Student*)malloc(sizeof(Student)*len);
for(int i = 0; i < len; i++){
strcpy(@@@, @@@);
@@@ = @@@;
}
qsort(students, len, sizeof(Student), compare);
return @@@;
}
// The following is main function to output testcase.
int main() {
char* names[5] = {"lukas", "james", "levi", "eli", "max"};
int names_len = 5;
int scores[5] = {30, 20, 50, 40, 10};//No scores tie
int scores_len = 5;
int K = 2;
char* ret = solution(names, names_len, scores, scores_len, K);
// Press Run button to receive output.
printf("Solution: return value of the function is %s .\n", ret);
}
C언어의 QSort
void qsort (void *base, size_t nel, size_t width, int (*compare)(const void *, const void *);
각 인수는
- base : 정렬하고자 하는 배열의 포인터
- nel : 배열의 총 길이
- width : 배열의 원소 하나의 크기
- (*compare) : 비교를 담당할 함수
qsort의 마지막 인수로 들어가는 compare 함수를 기억해놓자.
compare함수는 1,0,-1중 하나의 값을 반환한다.
오름차순으로 정렬을 원하는 경우
변수 비교 결과 | return |
one > two | 1 |
one < two | -1 |
one == two | 0 |
내림차순으로 정렬을 원하는 경우
변수 비교 결과 | return |
one > two | -1 |
one < two | 1 |
one == two | 0 |
정답 코드(YBM과 동일)
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
typedef struct Student{
char name[20];
int score;
}Student;
int compare(const void *a, const void * b){
Student first = *(Student *)a;
Student second = *(Student *)b;
if(first.score > second.score)
return -1;
else if(first.score < second.score)
return 1;
else
return 0;
}
char* solution(char* names[], int names_len, int scores[], int scores_len, int K) {
int len = names_len;
Student* students = (Student*)malloc(sizeof(Student)*len);
for(int i = 0; i < len; i++){
strcpy(students[i].name, names[i]);
students[i].score = scores[i];
}
qsort(students, len, sizeof(Student), compare);
//printf("%s",students[K-1].name);
return students[K-1].name;
}
int main() {
char* names[5] = {"lukas", "james", "levi", "eli", "max"};
int names_len = 5;
int scores[5] = {30, 20, 50, 40, 10};
int scores_len = 5;
int K = 2;
char* ret = solution(names, names_len, scores, scores_len, K);
printf("solution 함수의 반환 값은 %s 입니다.\n", ret);
}
KimSky904 - Overview
Department of New Media Software. KimSky904 has 28 repositories available. Follow their code on GitHub.
github.com
'[코테] > [GroomEdu]' 카테고리의 다른 글
[재업][COS PRO 2급] 2차 10번_상품권 총 지급액 구하기 (C/C++) (0) | 2021.05.29 |
---|---|
[COS PRO 2급] 6차 10번_사과 박스 무게의 불량 검사 (C/C++) (0) | 2021.05.29 |
[COS PRO 2급] 6차 9번_난 양말색이 달라도 잘 신는 착한 어린이 (C/C++) (0) | 2021.05.29 |
[COS PRO 2급] 6차 8번_주어진 수와 뒤집은 수의 차 구하기 (C/C++) (0) | 2021.05.29 |
[COS PRO 2급] 6차 7번_의자와 책상을 사고싶어요 (C/C++) (0) | 2021.05.29 |
- c언어 기출문제
- Java
- 구름에듀
- 구름 기출문제
- C
- 1급
- 배열활용문제
- lv2
- 구름에듀 기출문제
- 연습문제
- 배열
- 기출문제
- CosPro
- 자바
- groomedu
- 코딩테스트
- YBM
- c언어
- Cos Pro
- groom
- YBM기출
- 프로그래머스
- 알고리즘
- programmers
- lv1
- C++
- 코스프로
- cospro기출
- COSPRO 2급
- cospro기출문제
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |