반응형

 

문제 1

A 학교에서는 단체 티셔츠를 주문하기 위해 학생별로 원하는 티셔츠 사이즈를 조사했습니다. 선택할 수 있는 티셔츠 사이즈는 작은 순서대로 "XS", "S", "M", "L", "XL", "XXL" 총 6종류가 있습니다.

학생별로 원하는 티셔츠 사이즈를 조사한 결과가 들어있는 배열 shirt_size가 매개변수로 주어질 때, 사이즈별로 티셔츠가 몇 벌씩 필요한지 가장 작은 사이즈부터 순서대로 배열에 담아 return 하도록 solution 함수를 완성해주세요.

##### 매개변수 설명
학생별로 원하는 사이즈를 조사한 결과가 들어있는 배열 shirt_size가 solution 함수의 매개변수로 주어집니다.
* shirt_size 의 길이는 1 이상 100 이하입니다.
* shirt_size 에는 치수를 나타내는 문자열 "XS", "S", "M", "L", "XL", "XXL" 이 들어있습니다.

---
##### return 값 설명
티셔츠가 사이즈별로 몇 벌씩 필요한지 가장 작은 사이즈부터 순서대로 배열에 담아 return 해주세요.
* return 하는 배열에는 [ "XS" 개수, "S" 개수, "M" 개수, "L" 개수, "XL" 개수, "XXL" 개수] 순서로 들어있어야 합니다.

##### 예시

 shirt_size   return  
["XS", "S", "L", "L", "XL", "S"] | [1, 2, 0, 2, 1, 0] 

##### 예시 설명
* "XS"와 "XL"은 각각 한 명씩 신청했습니다.
* "S"와 "L"은 각각 두 명씩 신청했습니다.
* "M"과 "XXL"을 신청한 학생은 없습니다.

따라서 순서대로 [1, 2, 0, 2, 1, 0]을 배열에 담아 return 하면 됩니다.

[ 소스 코드 ]

// You may use include as below.
#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<string> shirt_size) {
    // Write code here.
    vector<int> answer;
    return answer;
}

// The following is main function to output testcase.
int main() {
    vector<string> shirt_size = {"XS", "S", "L", "L", "XL", "S"};
    vector<int> ret = solution(shirt_size);

    // Press Run button to receive output.
    cout << "Solution: return value of the function is {";
    for(int i = 0; i < ret.size(); i++){
        if (i != 0) cout << ", ";
        cout << ret[i];
    }
    cout << "} ." << endl;
    
    return 0;
}

[ 정답보기 ]

더보기
#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<string> shirt_size) {
    vector<int> size_counter(6, 0);
    for(int i = 0; i < shirt_size.size(); ++i)
    {
        if(shirt_size[i].compare("XS") == 0)
            size_counter[0]++;
        else if(shirt_size[i].compare("S") == 0)
            size_counter[1]++;
        else if(shirt_size[i].compare("M") == 0)
            size_counter[2]++;
        else if(shirt_size[i].compare("L") == 0)
            size_counter[3]++;
        else if(shirt_size[i].compare("XL") == 0)
            size_counter[4]++;
        else if(shirt_size[i].compare("XXL") == 0)
            size_counter[5]++;
    }
    return size_counter;
}

 

 

문제 2

A 쇼핑몰에서는 회원 등급에 따라 할인 서비스를 제공합니다.
회원 등급에 따른 할인율은 다음과 같습니다.
(S = 실버, G = 골드, V = VIP)

등급 할인율
"S" 5%  
"G" 10%
"V"  15%

상품의 가격 price와 구매자의 회원 등급을 나타내는 문자열 grade가 매개변수로 주어질 때, 할인 서비스를 적용한 가격을 return 하도록 solution 함수를 완성해주세요.

#####매개변수 설명
상품의 가격 price와 회원 등급 grade가 매개변수로 주어집니다.
* price는 100 이상 100,000 이하의 100단위 자연수입니다.
* grade는 "S", "G", "V" 세 가지 중 하나입니다.

---

#####return 값 설명
할인한 가격을 return 하도록 solution 함수를 작성해주세요.

---

#####예시

price grade  return
 2500 "V" 2125
 96900 "S" 92055 

##### 예시 설명
예시 #1
2500원의 15%는 375원 입니다. 2500 - 375 = 2125 입니다.

예시 #2
96900원의 5%는 4845원 입니다. 96900 - 4845 = 92055 입니다.

 

[ 소스 코드 ]

// You may use include as below.
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int solution(int price, string grade) {
    // Write code here.
    int answer = 0;
    return answer;
}

// The following is main function to output testcase.
int main() {
    int price1 = 2500;
    string grade1 = "V";
    int ret1 = solution(price1, grade1);
    
    // Press Run button to receive output.
    cout << "Solution: return value of the function is " << ret1 << " ." << endl;
    
    int price2 = 96900;
    string grade2 = "S";
    int ret2 = solution(price2, grade2);
    
    // Press Run button to receive output.
    cout << "Solution: return value of the function is " << ret2 << " ." << endl;
}

 

[ 정답 보기 ]

더보기
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int solution(int price, string grade) {
	int answer = 0;
	if (grade == "S")
		answer = price * 0.95;
	if (grade == "G")
		answer = price * 0.9;
	if (grade == "V")
		answer = price * 0.85;
	return answer;
}

 

 

문제 3

시작 날짜와 끝 날짜가 주어질 때, 두 날짜가 며칠만큼 떨어져 있는지(D-day)를 구하려 합니다. 이를 위해 다음과 같이 3단계로 간단히 프로그램 구조를 작성했습니다. (단, 윤년은 고려하지 않습니다.)

~~~
1단계. 시작 날짜가 1월 1일로부터 며칠만큼 떨어져 있는지 구합니다.
2단계. 끝 날짜가 1월 1일로부터 며칠만큼 떨어져 있는지 구합니다.
3단계. (2단계에서 구한 날짜) - (1단계에서 구한 날짜)를 구합니다.
~~~

시작 날짜의 월, 일을 나타내는 start_month, start_day, 끝 날짜의 월, 일을 나타내는 end_month, end_day가 매개변수로 주어질 때, 시작 날짜와 끝 날짜가 며칠만큼 떨어져 있는지 return 하도록 solution 함수를 작성했습니다. 이때, 위 구조를 참고하여 중복되는 부분은 func_a라는 함수로 작성했습니다. 코드가 올바르게 동작할 수 있도록 빈칸을 알맞게 채워주세요.

---
##### 매개변수 설명
시작 날짜의 월, 일을 나타내는 start_month, start_day, 끝 날짜의 월, 일을 나타내는 end_month, end_day가 solution 함수의 매개변수로 주어집니다.

* 잘못된 날짜가 주어지는 경우는 없습니다.
* 끝 날짜는 항상 시작 날짜보다 뒤에 있는 날이 주어집니다.
* 끝 날짜가 다음 해로 넘어가는 경우는 주어지지 않습니다.
  * 즉, start_month <= end_month를 항상 만족합니다.
  * start_month = end_month라면 start_day <= end_day를 항상 만족합니다.
* 각 달의 날짜 수는 1월부터 순서대로 [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] 이며, 윤년은 고려하지 않습니다.

---
##### return 값 설명
시작 날짜와 끝 날짜가 며칠만큼 떨어져 있는지 return 해주세요.

---
##### 예시

start_month  end_month  start_day end_day return
1 2 2 2 31

##### 예시 설명
시작 날짜는 1월 2일이고, 끝 날짜는 2월 2일입니다.

* 1월 2일은 1월 1일로부터 1일만큼 지난 날입니다.
* 2월 2일은 1월 1일로부터 32일만큼 지난 날입니다.
* 32 - 1 = 31입니다.
* 따라서 1월 2일과 2월 2일은 31일만큼 떨어져 있습니다.

[ 소스 코드 ]

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int func_a(int month, int day){
    int month_list[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int total = 0;
    for(int i = 0; i @@@; i++)
        total += @@@;
    total += @@@;
    return total - 1;
}

int solution(int start_month, int start_day, int end_month, int end_day) {
    int start_total = func_a(start_month, start_day);
    int end_total = func_a(end_month, end_day);
    return end_total - start_total;
}

// The following is main function to output testcase.
int main() {
    int start_month = 1;
    int start_day = 2;
    int end_month = 2;
    int end_day = 2;
    int ret = solution(start_month, start_day, end_month, end_day);
    
    // Press Run button to receive output.
    cout << "Solution: return value of the function is " << ret << " ." << endl;
}

 

[ 정답 보기 ]

더보기
#include <string>
#include <vector>

using namespace std;

int func_a(int month, int day){
    int month_list[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int total = 0;
    for(int i = 0; i < month - 1; ++i)
        total += month_list[i];
    total += day;
    return total - 1;
}

int solution(int start_month, int start_day, int end_month, int end_day) {
    int start_total = func_a(start_month, start_day);
    int end_total = func_a(end_month, end_day);
    return end_total - start_total;
}

 

 

문제 4

자연수가 들어있는 배열이 있습니다. 이 배열에서 가장 많이 등장하는 숫자의 개수는 가장 적게 등장하는 숫자 개수의 몇 배인지 구하려 합니다. 이를 위해 다음과 같이 간단히 프로그램 구조를 작성했습니다.

~~~
1단계. 배열에 들어있는 각 자연수의 개수를 셉니다.
2단계. 가장 많이 등장하는 수의 개수를 구합니다.
3단계. 가장 적게 등장하는 수의 개수를 구합니다.
4단계. 가장 많이 등장하는 수가 가장 적게 등장하는 수보다 몇 배 더 많은지 구합니다.
~~~

단, 몇 배 더 많은지 구할 때는 소수 부분은 버리고 정수 부분만 구하면 됩니다.

자연수가 들어있는 배열 arr가 매개변수로 주어질 때, 가장 많이 등장하는 숫자가 가장 적게 등장하는 숫자보다 몇 배 더 많은지 return 하도록 solution 함수를 작성하려 합니다. 위 구조를 참고하여 코드가 올바르게 동작할 수 있도록 빈칸에 주어진 func_a, func_b, func_c 함수와 매개변수를 알맞게 채워주세요.

---
##### 매개변수 설명
자연수가 들어있는 배열 arr가 solution 함수의 매개변수로 주어집니다.
* arr의 길이는 3 이상 1,000 이하입니다.
* arr에는 1 이상 1,000이하의 자연수가 들어있습니다.

---
##### return 값 설명
배열에서 가장 많이 등장하는 숫자가 가장 적게 등장하는 숫자보다 몇 배 이상 많은지 return 해주세요.

* 가장 많이 들어있는 수의 개수와 가장 적게 들어있는 수의 개수가 같은 경우에는 1을 return 합니다.

---
##### 예시

arr  return
 [1,2,3,3,1,3,3,2,3,2] 2

 

##### 예시 설명
배열에 1이 2개, 2가 3개, 3이 5개 들어있습니다.

* 가장 적게 들어있는 숫자 : 1 (2개)
* 가장 많이 들어있는 숫자 : 3 (5개)

3이 1보다 2.5배 많이 들어있으며, 소수 부분을 버리고 2를 return 하면 됩니다.

[ 소스 코드 ]

#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<int> func_a(vector<int> arr){
    vector<int> counter(1001, 0);
    for(int i = 0; i < arr.size(); i++)
        counter[arr[i]]++;
    return counter;
}

int func_b(vector<int> arr){
    int ret = 0;
    for(int i = 0; i < arr.size(); i++){
        if(ret < arr[i])
            ret = arr[i];
    }
    return ret;
}

int func_c(vector<int> arr){
    const int INF = 1001;
    int ret = INF;
    for(int i = 0; i < arr.size(); i++){
        if(arr[i] != 0 && ret > arr[i])
            ret = arr[i];
    }
    return ret;
}

int solution(vector<int> arr) {
    vector<int> counter = func_@@@(@@@);
    int max_cnt = func_@@@(@@@);
    int min_cnt = func_@@@(@@@);
    return max_cnt / min_cnt;
}

// The following is main function to output testcase.
int main() {
    vector<int> arr = {1, 2, 3, 3, 1, 3, 3, 2, 3, 2};
    int ret = solution(arr);

    // Press Run button to receive output.
    cout << "Solution: return value of the function is " << ret << " ." << endl;
}

 

[ 정답 보기 ]

더보기
#include <string>
#include <vector>

using namespace std;

vector<int> func_a(vector<int> arr){
    vector<int> counter(1001, 0);
    for(int i = 0; i < arr.size(); ++i)
        counter[arr[i]]++;
    return counter;
}

int func_b(vector<int> arr){
    int ret = 0;
    for(int i = 0; i < arr.size(); ++i){
        if(ret < arr[i])
            ret = arr[i];
    }
    return ret;
}

int func_c(vector<int> arr){
    int ret = 1001;
    for(int i = 0; i < arr.size(); ++i){
        if(arr[i] != 0 && ret > arr[i])
            ret = arr[i];
    }
    return ret;
}

int solution(vector<int> arr) {
    vector<int> counter = func_a(arr);
    int max_cnt = func_b(counter);
    int min_cnt = func_c(counter);
    return max_cnt / min_cnt;
}

 

문제 5

주어진 배열의 순서를 뒤집으려고 합니다.

예를 들어 주어진 배열이 [1, 4, 2, 3]이면, 순서를 뒤집은 배열은 [3, 2, 4, 1]입니다.

정수가 들어있는 배열 arr가 매개변수로 주어졌을 때, arr를 뒤집어서 return 하도록 solution 함수를 작성하려 합니다. 빈칸을 채워 전체 코드를 완성해주세요.

---
##### 매개변수 설명
정수가 들어있는 배열 arr가 solution 함수의 매개변수로 주어집니다.
* arr의 길이는 1 이상 100 이하입니다.
* arr의 원소는 -100 이상 100 이하의 정수입니다.

---
##### return 값 설명
배열 arr의 순서를 뒤집어서 return 해주세요.

---
##### 예시

arr return
 [1, 4, 2, 3] [3, 2, 4, 1]

##### 예시 설명
[1, 4, 2, 3]을 뒤에서부터 읽으면 3, 2, 4, 1입니다. 따라서 [1, 4, 2, 3]의 순서를 뒤집은 결과는 [3, 2, 4, 1]이 됩니다.

 

[ 소스 코드 ]

#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<int> arr) {
    int left = 0;
    int right = arr.size() - 1;
    while(@@@){
        int temp = arr[left];
        arr[left] = arr[right];
        arr[right] = temp;
        left += 1;
        right -= 1;
    }
    return arr;
}


// The following is main function to output testcase.
int main() {
    vector<int> arr = {1, 4, 2, 3};
    vector<int> ret = solution(arr);

    // Press Run button to receive output.
    cout << "Solution: return value of the function is {";
    for(int i = 0; i < ret.size(); i++){
        if (i != 0) cout << ", ";
        cout << ret[i];
    }
    cout << "} ." << endl;
}

 

[ 정답보기 ]

더보기
#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<int> arr) {
    int left = 0;
    int right = arr.size() - 1;
    while(left < right){
        int tmp = arr[left];
        arr[left] = arr[right];
        arr[right] = tmp;
        left += 1;
        right -= 1;
    }
    return arr;
}

 

[ 출처 ] www.ybmit.com   cos pro 샘플문제

반응형
Posted by 명문코딩컴퓨터
,