반응형

 

문제 1

왼손 장갑의 제품 번호가 들어있는 배열과 오른손 장갑의 제품 번호가 들어있는 배열이 있습니다. 제품 번호는 1부터 10 사이의 자연수입니다. 제품 번호가 같은 왼손장갑과 오른손 장갑을 합쳐 장갑 한 쌍을 만들 수 있습니다. 이때, 최대한 많은 쌍의 장갑을 만들면 최대 몇 쌍을 만들 수 있는지 구하려 합니다. 이를 위해 다음과 같이 프로그램 구조를 작성했습니다. 

~~~
1. 왼손 장갑이 제품 번호별로 몇 개씩 있는지 개수를 셉니다.
2. 오른손 장갑이 제품 번호별로 몇 개씩 있는지 개수를 셉니다.
3. 각 제품 번호별로 최대한 많은 장갑 쌍을 만들면서 개수를 셉니다.
~~~

왼손 장갑의 제품 번호가 들어있는 배열 left_gloves와 오른손 장갑의 제품 번호가 들어있는 배열 right_gloves가 매개변수로 주어질 때, 최대 몇 개의 장갑 쌍을 만들 수 있는지 return 하도록 solution 함수를 작성하려 합니다. 이때, 위 구조를 참고하여 중복되는 부분은 func_a라는 함수로 작성했습니다. 코드가 올바르게 동작할 수 있도록 빈칸을 알맞게 채워주세요.

---
##### 매개변수 설명
왼손 장갑의 제품 번호가 들어있는 배열 left_gloves와 오른손 장갑의 제품 번호가 들어있는 배열 right_gloves가 solution 함수의 매개변수로 주어집니다.

* left_gloves의 길이는 1 이상 100 이하입니다.
* left_gloves의 원소는 1 이상 10 이하의 자연수입니다.
* right_gloves의 길이는 1 이상 100 이하입니다.
* right_gloves의 원소는 1 이상 10 이하의 자연수입니다.

---
##### return 값 설명 
왼손과 오른손의 제품 번호가 같은 장갑 끼리 한 쌍을 만들 때, 최대 몇 개의 쌍을 만들 수 있는지 개수를 return 해주세요.

---
##### 예시

left_gloves right_gloves return
 [2, 1, 2, 2, 4] [1, 2, 2, 4, 4, 7] 4

##### 예시 설명
예시 #1
왼손 장갑 : 1번 장갑 1개, 2번 장갑 3개, 4번 장갑 1개가 있습니다.
오른손 장갑 : 1번 장갑 1개, 2번 장갑 2개, 4번 장갑 2개, 7번 장갑 1개가 있습니다.

따라서 1번 장갑 한 쌍, 2번 장갑 두 쌍, 4번 장갑 한 쌍을 만들면 최대 4개의 장갑 쌍을 만들 수 있습니다.

[ 소스 코드 ]

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

using namespace std;

const int max_product_number = 10;

vector<int> func_a(vector<int> gloves){
    int gloves_len = gloves.size();
    vector<int> counter(max_product_number + 1, 0);
    
    for(int i = 0; i < gloves_len; ++i)
        @@@
    
    return counter;
}

int min(int a, int b){
    return a < b ? a : b;
}

int solution(vector<int> left_gloves, vector<int> right_gloves) {
    vector<int> left_counter = func_a(left_gloves);
    vector<int> right_counter = func_a(right_gloves);
    int total = 0;
    for(int i = 1; i <= max_product_number; ++i)
        total += min(left_counter[i], right_counter[i]);
    return total;
}

int main() {
    vector<int> left_gloves = {2, 1, 2, 2, 4};
    vector<int> right_gloves = {1, 2, 2, 4, 4, 7};
    int ret = solution(left_gloves, right_gloves);

       cout << "solution " << ret <<  endl;
}

 

[  정답보기 ]

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

using namespace std;

const int max_product_number = 10;

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

int min(int a, int b){
    return a < b ? a : b;
}

int solution(vector<int> left_gloves, vector<int> right_gloves) {
    vector<int> left_counter = func_a(left_gloves);
    vector<int> right_counter = func_a(right_gloves);
    int total = 0;
    for(int i = 1; i <= max_product_number; ++i)
        total += min(left_counter[i], right_counter[i]);
    return total;
}

 

 

문제 2

자연수가 들어있는 배열에 3의 배수와 5의 배수 중 어떤 수가 더 많은지 알아보려 합니다. 이를 위해 다음과 같이 프로그램 구조를 작성했습니다.

~~~
1. 3의 배수의 개수를 셉니다.
2. 5의 배수의 개수를 셉니다.
3. 3의 배수와 5의 배수의 개수를 비교 후 다음을 수행합니다.
  3-1. 만약 3의 배수가 더 많다면 "three"를 return 합니다.
  3-2. 만약 5의 배수가 더 많다면 "five"를 return 합니다.
  3-3. 만약 3의 배수와 5의 배수의 개수가 같다면 "same"을 return 합니다.
~~~

자연수가 들어있는 배열 arr가 매개변수로 주어질 때, 배열에 3의 배수의 개수가 더 많다면 "three"를, 5의 배수의 개수가 더 많다면 "five"를, 3의 배수와 5의 배수의 개수가 같다면 "same"을 return 하도록 solution 함수를 작성하려 합니다. 위 구조를 참고하여 코드가 올바르게 동작할 수 있도록 빈칸에 주어진 func_a, func_b, func_c 함수와 매개변수를 알맞게 채워주세요.

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

---
#####return 값 설명
배열에 3의 배수의 개수가 더 많다면 "three"를, 5의 배수의 개수가 더 많다면 "five"를, 3의 배수와 5의 배수의 개수가 같다면 "same"을 return 해주세요.

---
#####예시

arr  return
 [2, 3, 6, 9, 12, 15, 10, 20, 22, 25]  "three"

#####예시 설명

* 3의 배수 : 3, 6, 9, 12, 15
* 5의 배수 : 10, 15, 20, 25

3의 배수는 5개, 5의 배수는 4개이므로 3의 배수가 더 많습니다. 따라서 "three"를 return 합니다.

 

[ 소스 코드 ]

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

using namespace std;

int func_a(vector<int> arr){
    int count = 0;
    int length = arr.size();
    for(int i = 0; i < length; ++i)
        if (arr[i] % 5 == 0)
            count += 1;
    return count;
}

string func_b(int three, int five){
    if (three > five)
        return "three";
    else if (three < five)
        return "five";
    else
        return "same";
}

int func_c(vector<int> arr){
    int count = 0;
    int length = arr.size();
    for(int i = 0; i < length; ++i)
        if (arr[i] % 3 == 0)
            count += 1;
    return count;
}

string solution(vector<int> arr) {
    int count_three = func_@@@(@@@);
    int count_five = func_@@@(@@@);
    string answer = func_@@@(@@@);
    return answer;
}

int main() {
    vector<int> arr = {2, 3, 6, 9, 12, 15, 10, 20, 22, 25};
    string ret = solution(arr);

    cout << "solution " << ret << endl;
}

 

[  정답보기 ]

더보기

 

#include <string>
#include <vector>

using namespace std;

int func_a(vector<int> arr){
    int count = 0;
    int length = arr.size();
    for(int i = 0; i < length; ++i)
        if (arr[i] % 5 == 0)
            count += 1;
    return count;
}

string func_b(int three, int five){
    if (three > five)
        return "three";
    else if (three < five)
        return "five";
    else
        return "same";
}

int func_c(vector<int> arr){
    int count = 0;
    int length = arr.size();
    for(int i = 0; i < length; ++i)
        if (arr[i] % 3 == 0)
            count += 1;
    return count;
}

string solution(vector<int> arr) {
    int count_three = func_c(arr);
    int count_five = func_a(arr);
    string answer = func_b(count_three, count_five);
    return answer;
}

 

문제 3

서로 다른 두 자연수 N과 M이 매개변수로 주어질 때, N부터 M까지의 자연수 중에서 짝수들의 제곱의 합을 return 하도록 solution 함수를 완성해주세요.

---
#####매개변수 설명
두 자연수 N과 M이 solution 함수의 매개변수로 주어집니다.
* N과 M은 1 이상 1,000 이하의 자연수이며, N < M을 항상 만족합니다.

---
#####return 값 설명
N부터 M까지의 수 중에서 짝수인 수의 제곱의 합을 return 해주세요.

---
#####예시

N M return 
4 7 52  

#####예시 설명
4부터 7까지의 자연수 중에서 짝수는 4와 6입니다.

* 4^2 + 6^2 = 16 + 36 = 52

따라서 52를 return 하면 됩니다.

 

[ 소스 코드 ]

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

using namespace std;

int solution(int N, int M) {
    int answer = 0;
    return answer;
}

int main() {
    int N = 4;
    int M = 7;
    int ret = solution(N, M);

   cout << "solution " << ret << endl;
    
    return 0;
}

 

[ 정답보기 ]

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

using namespace std;

int solution(int N, int M) {
    int total = 0;
    for(int i = N; i <= M; ++i){
        if(i % 2 == 0)
            total += i*i;
    }
    return total;
}

 

문제 4

단어들이 들어있는 배열에서 길이가 5 이상인 단어를 배열에 들어있는 순서대로 이어 붙이려 합니다.

예를 들어 배열이 다음과 같은 경우

["my", "favorite", "color", "is", "violet"]

"favoritecolorviolet"을 만들면 됩니다.

단어들이 들어있는 배열 words가 solution 함수의 매개변수로 주어질 때, 길이가 5 이상인 단어를 순서대로 이어 붙인 문자열을 return 하도록 solution 함수를 완성해주세요.

---
#####매개변수 설명
단어들이 들어있는 배열 words가 solution 함수의 매개변수로 주어집니다.

* words의 길이는 1 이상 100 이하입니다.
* words에 들어있는 각 단어의 길이는 1 이상 10 이하이며, 알파벳 소문자로만 이루어져 있습니다.

---
#####return 값 설명
길이가 5 이상인 단어를 순서대로 이어 붙여 return 해주세요.
* 만약 return 할 문자열이 빈 문자열이면 "empty"를 return 해주세요.

---
#####예시

words return
["my", "favorite", "color", "is", "violet"] | "favoritecolorviolet" |
| ["yes", "i", "am"] 
"empty"

#####예시 설명
예시 #1
길이가 5 이상인 단어는 "favorite", "color", "violet" 입니다. 이를 배열에 들어있는 순서대로 이어 붙이면 "favoritecolorviolet"이 됩니다.

예시 #2
길이가 5 이상인 단어가 없으므로 "empty"를 return 하면 됩니다.

 

[ 소스 코드 ]

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

using namespace std;

string solution(vector<string> words) {
    string answer = "";
    return answer;
}

int main() {
    vector<string> words1 = {"my", "favorite", "color", "is", "violet"};
    string ret1 = solution(words1);

     cout << "solution ?" << ret1 << endl;
    
    vector<string> words2 = {"yes", "i", "am"};
    string ret2 = solution(words2);

   cout << "solution? " << ret2 << endl;

    return 0;
}

 

[ 정답 보기 ]

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

using namespace std;

string solution(vector<string> words) {
    string answer = "";
    for(auto w : words)
        if(w.length() >= 5)
            answer += w;
    if(answer.length() < 1)
        answer = "empty";
    return answer;
}

 

 

문제5

게임 캐릭터가 몬스터와 1:1 전투를 하려 합니다. 몬스터는 처음에 일정 수치의 체력(HP)을 가지고 있습니다. 캐릭터가 전투에서 이기려면 몬스터를 공격해 몬스터의 체력을 0이하로 만들어야합니다. 캐릭터는 공격 마법만 사용하며, 공격 마법은 항상 같은 데미지를 입힙니다. 몬스터는 힐링 마법만을 사용하며, 힐링 마법은 항상 같은 수치로 체력을 회복합니다. 둘은 항상 번갈아 가며 마법을 사용하고, 처음에는 항상 캐릭터가 먼저 공격을 시작합니다.

캐릭터의 공격력 attack과 몬스터가 자신의 차례에 회복하는 체력 recovery, 몬스터의 초기 체력 hp가 매개변수로 주어질 때, 몬스터를 잡기 위해서 최소 몇 번 공격해야 하는지 return 하도록 solution 함수를 작성하려 합니다. 빈칸을 채워 전체 코드를 완성해주세요.

---
#####매개변수 설명
캐릭터의 공격력 attack과 몬스터가 자신의 차례에 회복하는 체력 recovery, 몬스터의 초기 체력 hp가 solution 함수의 매개변수로 주어집니다.

* attack은 1 이상 100 이하의 자연수입니다.
* recovery는 1 이상 100 이하의 자연수입니다.
* 캐릭터의 공격력은 항상 몬스터의 회복력보다 큽니다(recovery < attack).
* hp는 200 이상 1,000 이하의 자연수입니다.

---
#####return 값 설명
몬스터를 잡기 위해서 최소 몇 번 공격해야 하는지 return 해주세요.

---
#####예시

attack recovery hp return
30 10  60 3

 

#####예시 설명

몬스터의 체력 변화는 아래와 같습니다.

차례 hp 변화 남은 hp
캐릭터 없음 60  
몬스터 공격 -30  30
캐릭터 회복 +10  40 
몬스터 공격 -30 10
 캐릭터 회복 +10 20
몬스터 공격 -30  -10

따라서 최소 3번 공격해야 몬스터를 잡을 수 있습니다.

 

[ 소스 코드 ]

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

using namespace std;

int solution(int attack, int recovery, int hp) {
    int count = 0;
    while(true){
        count += @@@;
        hp -= @@@;
        if(hp <= 0)
            @@@;
        hp += @@@;
    }
    return count;
}

int main() {
    int attack = 30;
    int recovery = 10;
    int hp = 60;
    int ret = solution(attack, recovery, hp);

    cout << "solution " << ret  << endl;
}

 

[ 정답 보기 ]

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

using namespace std;

int solution(int attack, int recovery, int hp) {
    int count = 0;
    while(true){
        count += 1;
        hp -= attack;
        if(hp <= 0)
            break;
        hp += recovery;
    }
    return count;
}

 

 

 

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

 

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