반응형

 

문제 6

369 게임은 여러 명이 같이하는 게임입니다. 게임의 규칙은 아래와 같습니다.
* 1부터 시작합니다.
* 한 사람씩 차례대로 숫자를 1씩 더해가며 말합니다.
* 말해야 하는 숫자에 3, 6, 9중 하나라도 포함되어있다면 숫자를 말하는 대신 숫자에 포함된 3, 6, 9의 개수만큼 손뼉을 칩니다.

어떤 수 number가 매개변수로 주어질 때, 1부터 number까지 369게임을 올바르게 진행했을 경우 박수를 총 몇 번 쳤는지를 return 하도록 solution 함수를 작성하려 합니다. 빈칸을 채워 전체 코드를 완성해주세요.

---

#####매개변수 설명
number가 solution 함수의 매개변수로 주어집니다.
* number는 10 이상 1,000 이하의 자연수입니다.

---

#####return 값 설명
1부터 number까지 369게임을 올바르게 진행했을 경우 박수를 총 몇 번을 쳤는지 return 해주세요.

---

#####예시

number return
40 22

 

#####예시 설명
3, 6, 9 : 각각 한 번 (+3)
13, 16, 19 : 각각 한 번 (+3)
23, 26, 29 : 각각 한 번 (+3)
30, 31, 32, 33, ..., 38, 39 : 십의 자리 열 번 + 일의 자리 세 번 (+13)
따라서, 3 + 3 + 3 + 13 = 22번의 박수를 칩니다.

 

[ 소스 코드]

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

using namespace std;

int solution(int number) {
    int count = 0;
    for (int i = 1; i <= number; i++) {
        int current = i;
        int temp = count;
        while (current != 0) {
            if (@@@){
                count++;
                cout << "pair";
            }
            current /= 10;
        }
        if(temp == count)
            cout << i;
        cout << " ";
    }
    cout << endl;
    return count;
}

// The following is main function to output testcase.
int main() {
    int number = 40;
    int ret = solution(number);

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

 

[ 정답 보기 ]

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

using namespace std;

int solution(int number) {
	int count = 0;
	for (int i = 1; i <= number; i++) {
		int current = i;
		while (current != 0) {
			if (current % 10 == 3 || current % 10 == 6 || current % 10 == 9)
				count++;
			current /= 10;
		}
	}
	return count;
}

 

문제 7

A 대학에서는 수준별 영어 강의를 제공하고 있습니다. 초급 영어 강의는 토익시험에서 650점 이상 800점 미만의 성적을 취득한 학생만을 수강대상으로 하고 있습니다. 초급 영어 강의에 수강신청한 사람이 10명일 때, 이 중에서 몇명이 수강 대상에 해당하는지 확인하려합니다.

수강신청자들의 토익 성적이 들어있는 배열 scores가 매개변수로 주어질 때, 수강 대상자들의 인원수를 return 하도록 solution 함수를 작성했습니다. 그러나, 코드 일부분이 잘못되어있기 때문에, 몇몇 입력에 대해서는 올바르게 동작하지 않습니다. 주어진 코드에서 _**한 줄**_만 변경해서 모든 입력에 대해 올바르게 동작하도록 수정해주세요.

---

#####매개변수 설명
수강신청자들의 토익 성적이 들어있는 배열 scores가 solution 함수의 매개변수로 주어집니다.
* scores의 원소는 500 이상 990 이하의 정수입니다.
* scores의 길이는 10입니다.

---

#####return 값 설명
수강 대상자들의 인원수를 return 해주세요.

---

#####예시

scores  return 
 [650, 722, 914, 558, 714, 803, 650, 679, 669, 800]  6

 

#####예시 설명

점수 650 722 914  558 714 803 650 679 669   800
수강대상 O O X X O X O O O X

650점 이상 800점 미만의 성적을 취득한 학생이 수강대상이므로, 800점을 취득한 학생은 수강대상이 아닙니다.
따라서, 6명이 수강 대상입니다.

[ 소스 코드 ]

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

using namespace std;

int solution(vector<int> scores) {
    int count = 0;
    for (int i = 0; i < scores.size(); i++)
        if (650 <= scores[i] || scores[i] < 800) 
            count += 1;
    return count;
}

// The following is main function to output testcase. The main function is correct and you shall correct solution function.
int main() {
    vector<int> scores = {650, 722, 914, 558, 714, 803, 650, 679, 669, 800};
    int ret = solution(scores);

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

 

[ 정답 보기 ]

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

using namespace std;

int solution(vector<int> scores) {
	int count = 0;
	for (int i = 0; i < scores.size(); i++)
		if (650 <= scores[i] && scores[i] < 800) 
			count += 1;
	return count;
}

 

 

문제 8

앞에서부터 읽을 때와 뒤에서부터 읽을 때 똑같은 단어 또는 문장을 팰린드롬(palindrome)이라고 합니다. 예를 들어서 racecar, noon은 팰린드롬 단어입니다. 

소문자 알파벳, 공백(" "), 그리고 마침표(".")로 이루어진 문장이 팰린드롬 문장인지 점검하려 합니다. 문장 내에서 알파벳만 추출하였을 때에 팰린드롬 단어이면 팰린드롬 문장입니다. 예를 들어, "Never odd or even."과 같은 문장은 팰린드롬입니다.

소문자 알파벳, 공백(" "), 그리고 마침표(".")로 이루어진 문장 sentence가 주어질 때 팰린드롬인지 아닌지를 return 하도록 solution 함수를 작성했습니다. 그러나, 코드 일부분이 잘못되어있기 때문에, 몇몇 입력에 대해서는 올바르게 동작하지 않습니다. 주어진 코드에서 _**한 줄**_만 변경해서 모든 입력에 대해 올바르게 동작하도록 수정해주세요.

---
##### 매개변수 설명
소문자 알파벳, 공백(" "), 그리고 마침표(".")로 이루어진 문장 sentence가 solution 함수의 매개변수로 주어집니다.

* sentence의 길이는 1이상 100이하입니다.
* sentence에는 적어도 하나의 알파벳이 포함되어 있습니다.
* setntence의 각 문자는 소문자 알파벳, 공백(" "), 또는 마침표(".")입니다.

---
##### return 값 설명
주어진 문장이 팰린드롬인지 아닌지를 return 해주세요.

---
##### 예시

sentence  return
"never odd or even."  true   
"palindrome"   false 

##### 예시 설명
예시 #1
알파벳과 숫자만 추출하여 소문자로 변환해보면 "neveroddoreven"이 되며 이 단어는 팰린드롬입니다.

예시 #2
문장의 맨 앞 문자인 "p"와 맨 뒤 문자인 "e"가 다르므로 팰린드롬이 아닙니다.

 

[ 소스 코드 ]

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

using namespace std;

bool solution(string sentence) {
    string alphas = "";
    for (char ch : sentence) {
        if (ch != '.' || ch != ' ') alphas += ch;
    }
    int len = alphas.size();
    for (int i = 0; i < len / 2; i++) {
        if (alphas[i] != alphas[len - 1 - i]) return false;
    }
    return true;
}

// The following is main function to output testcase. The main function is correct and you shall correct solution function.
int main() {
    string sentence1 = "never odd or even.";
    bool ret1 = solution(sentence1);
    
    // Press Run button to receive output.
    cout << "Solution: return value of the function is " << (ret1 == true ? "true" : "false") << " ." << endl;
    

    string sentence2 = "palindrome";
    bool ret2 = solution(sentence2);
    
    // Press Run button to receive output.
    cout << "Solution: return value of the function is " << (ret2 == true ? "true" : "false") << " ." << endl;
}

 

[ 정답 보기 ]

더보기
#include <string>
using namespace std;
bool solution(string sentence) {
	string alphas = "";
	for (char ch : sentence) {
		if (ch != '.' && ch != ' ') alphas += ch;
	}
	int len = alphas.size();
	for (int i = 0; i < len / 2; i++) {
		if (alphas[i] != alphas[len - 1 - i]) return false;
	}
	return true;
}

 

문제 9


알파벳 문자열이 주어질 때, 연속하는 중복 문자를 삭제하려고 합니다. 예를 들어, "senteeeencccccceeee"라는 문자열이 주어진다면, "sentence"라는 결과물이 나옵니다.

영어 소문자 알파벳으로 이루어진 임의의 문자열 characters가 매개변수로 주어질 때, 연속하는 중복 문자들을 삭제한 결과를 return 하도록 solution 함수를 작성하였습니다. 그러나, 코드 일부분이 잘못되어있기 때문에, 코드가 올바르게 동작하지 않습니다. 주어진 코드에서 _**한 줄**_만 변경해서 모든 입력에 대해 올바르게 동작하도록 수정하세요.

---

#####매개변수 설명
영어 소문자 알파벳으로 이루어진 임의의 문자열 characters가 solution 함수의 매개변수로 주어집니다. 
* characters는 알파벳 소문자로만 이루어져있습니다.
* characters의 길이는 10 이상 100 이하입니다.

---

#####return 값 설명
characters에서 연속하는 중복 문자를 제거한 문자열을 return 해주세요.

---

#####예시

characters  return 
"senteeeencccccceeee" "sentence"

 

[ 소스 코드 ]

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

using namespace std;

string solution(string characters) {
    string result = "";
    result += characters[0];
    for (int i = 0; i < characters.length(); i++) {
        if (characters[i - 1] != characters[i]) {
            result += characters[i];
        }
    }
    return result;
}


// The following is main function to output testcase. The main function is correct and you shall correct solution function.
int main() {
    string characters = "senteeeencccccceeee";
    string ret = solution(characters);

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

 

[ 정답 보기]

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

using namespace std;

string solution(string characters) {
	string result = "";
	result += characters[0];
	for (int i = 1; i < characters.length(); i++) {
		if (characters[i - 1] != characters[i]) {
			result += characters[i];
		}
	}
	return result;
}

 

 

문제 10

평균은 자료의 합을 자료의 개수로 나눈 값을 의미합니다. 자연수가 들어있는 배열의 평균을 구하고, 평균 이하인 숫자는 몇 개 있는지 구하려합니다.

예를 들어 주어진 배열이 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]이라면, 평균은 5.5이므로 배열에서 평균 이하인 값은 5개입니다.

자연수가 들어있는 배열 data가 매개변수로 주어질 때, 배열에 평균 이하인 값은 몇 개인지 return 하도록 solution 함수를 작성했습니다. 그러나, 코드 일부분이 잘못되어있기 때문에, 몇몇 입력에 대해서는 올바르게 동작하지 않습니다. 주어진 코드에서 한 줄만 변경해서 모든 입력에 대해 올바르게 동작하도록 수정하세요.


---

##### 매개변수 설명

자연수가 들어있는 배열 data가 solution 함수의 매개변수로 주어집니다.

* data의 길이는 10 이상 100 이하 정수입니다.
* data의 원소는 1 이상 1,000 이하의 자연수입니다.


---

##### return 값 설명

평균보다 값이 작은 자연수는 몇개인지 return 해주세요.

---

##### 예시

data return
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  5 
[1, 1, 1, 1, 1, 1, 1, 1, 1, 10]   9

##### 예시 설명

예시 #1
자료의 합은 55이며, 자료의 개수는 10개입니다. 따라서 평균은 55 / 10 = 5.5입니다.
주어진 배열에서 5.5보다 작은 숫자는 총 5개입니다.

예시 #2
자료의 합은 19이며, 자료의 개수는 10개입니다. 따라서 평균은 19 / 10 = 1.9입니다.
주어진 배열에서 1.9보다 작은 숫자는 총 9개입니다.

[ 소스 코드 ]

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

using namespace std;

int solution(vector<int> data) {
    double total = 0;
    int len = data.size();
    for(int i = 0; i < len; i++)
        total += data[i];
    int cnt = 0;
    double average = len / total;
    for(int i = 0; i < len; i++)
        if(data[i] <= average)
            cnt += 1;
    return cnt;
}

// The following is main function to output testcase. The main function is correct and you shall correct solution function.
int main() {
    vector<int> data1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int ret1 = solution(data1);
    
    // Press Run button to receive output.
    cout << "Solution: return value of the function is " << ret1 << " ." << endl;
    
    vector<int> data2 = {1, 1, 1, 1, 1, 1, 1, 1, 1, 10};
    int ret2 = solution(data2);
    
    // 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(vector<int> data) {
    double total = 0;
    int len = data.size();
    for(int i = 0; i < len; ++i)
        total += data[i];
    int cnt = 0;
    double average = (double)total / (double)len;
    for(int i = 0; i < len; ++i)
        if(data[i] <= average)
            cnt += 1;
    return cnt;
}

 

 

 

 

 

 

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

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