3-2. 정수들의 집단에서 사분위수들을 찾아서 출력하는 프로그램을 작성해보세요.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
using std::cout; | |
using std::cin; | |
using std::endl; | |
using std::vector; | |
int main(void) | |
{ | |
vector<int> numbers; | |
int x; | |
while (cin >> x) | |
{ | |
numbers.push_back(x); | |
} | |
if (numbers.size() < 4) | |
{ | |
cout << "정수의 개수가 4개 이상 있어야 합니다." << endl; | |
return 1; | |
} | |
if (numbers.size() % 4 != 0) | |
{ | |
cout << "사분위수들을 찾기 위해서는 정수의 개수가 4의 배수여야 합니다." << endl; | |
return 1; | |
} | |
sort(numbers.begin(), numbers.end()); | |
for (int i = 0; i < 3; i++) | |
{ | |
int idx = numbers.size() / 4 * (i + 1) - 1; | |
cout << i + 1 << "번째 사분위 수 : " << numbers[idx] << endl; | |
} | |
return 0; | |
} |

3-3. 입력에서 각 단어가 등장한 횟수를 세는 프로그램을 작성해보세요.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <algorithm> | |
using std::cout; | |
using std::cin; | |
using std::endl; | |
using std::vector; | |
using std::string; | |
int main(void) | |
{ | |
vector<string> words; | |
string s; | |
while (cin >> s) | |
{ | |
if (s == "finish") | |
{ | |
break; | |
} | |
words.push_back(s); | |
} | |
if (words.size() == 0) | |
{ | |
cout << "단어가 적어도 하나는 있어야 합니다." << endl; | |
return 1; | |
} | |
sort(words.begin(), words.end()); | |
string temp = words[0]; | |
int cnt = 1; | |
for (int i = 1; i < words.size(); i++) | |
{ | |
if (words[i] == temp) | |
{ | |
cnt++; | |
continue; | |
} | |
cout << temp << " : " << cnt << endl; | |
temp = words[i]; | |
cnt = 1; | |
} | |
cout << temp << " : " << cnt << endl; | |
return 0; | |
} |

3-4. 입력에서 길이가 가장 긴 문자열의 길이와 가장 짧은 문자열의 길이를 알려주는 프로그램을 작성해보세요.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <algorithm> | |
using std::cout; | |
using std::cin; | |
using std::endl; | |
using std::string; | |
using std::vector; | |
/* | |
문자열 길이 오름차순으로 정렬 | |
*/ | |
bool cmp(string a, string b) | |
{ | |
return a.length() < b.length(); | |
} | |
int main(void) | |
{ | |
vector<string> words; | |
string s; | |
while (cin >> s) | |
{ | |
if (s == "finish") | |
{ | |
break; | |
} | |
words.push_back(s); | |
} | |
sort(words.begin(), words.end(), cmp); | |
vector<string>::size_type lastIndex = words.size() - 1; | |
cout << "가장 짧은 길이 : " << words[0] << endl; | |
cout << "가장 긴 길이 : " << words[lastIndex] << endl; | |
return 0; | |
} |

3-5. 한 번에 여러 학생의 성적을 기록하는 프로그램을 작성해보세요. 프로그램은 벡터 2개를 동기화해 처리할 수 있습니다. 첫 번째 벡터는 학생 이름을 저장하고, 두 번째 벡터는 입력을 읽어 들여 계산한 최종 점수를 저장해야 합니다. 과제 점수 개수는 고정되어야 합니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <string> | |
#include <algorithm> | |
using std::cout; | |
using std::cin; | |
using std::endl; | |
using std::vector; | |
using std::string; | |
int main(void) | |
{ | |
vector<string> studentNames; | |
vector<int> totalPoints; | |
int N; | |
cout << "학생 당 과제 점수 개수 : "; | |
cin >> N; | |
string name; | |
while(cin >> name) | |
{ | |
if (name == "finish") | |
{ | |
break; | |
} | |
studentNames.push_back(name); | |
int total = 0; | |
for (int i = 0; i < N; i++) | |
{ | |
int point; | |
cin >> point; | |
total += point; | |
} | |
totalPoints.push_back(total); | |
} | |
vector<string>::size_type numberOfStudents = studentNames.size(); | |
for (vector<string>::size_type index = 0; index < numberOfStudents; index++) | |
{ | |
cout << "이름 : " << studentNames[index] << endl; | |
cout << "총합점수 : " << totalPoints[index] << endl << endl; | |
} | |
return 0; | |
} |

3-6. 만약 학생의 점수가 모두 입력되지 않으면 31/76p에서 살펴본 평균 점수 계산은 0으로 나눌 수 있습니다. 0으로 나누는 것은 C++에서 정의되지 않은 동작이므로 구현체마다 정해진 동작을 실행합니다. 이 때 여러분이 사용하는 C++ 구현체는 어떤 동작을 실행하는지 살펴보세요. 0으로 나누는 동작이 구현체의 정해진 방식을 따르지 않게 프로그램을 다시 작성해보세요.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iomanip> | |
#include <ios> | |
#include <iostream> | |
#include <string> | |
using std::cin; | |
using std::cout; | |
using std::endl; | |
using std::setprecision; | |
using std::string; | |
using std::streamsize; | |
int main(void) | |
{ | |
cout << "이름을 입력하세요 : "; | |
string name; | |
cin >> name; | |
cout << "안녕하세요, " << name << "님!" << endl; | |
cout << "중간고사와 기말고사 점수를 입력하세요 : "; | |
double midtermExam, finalExam; | |
cin >> midtermExam >> finalExam; | |
cout << "과제 점수를 모두 입력하세요, 입력을 마쳤다면 EOF를 입력하세요 : "; | |
int count = 0; | |
double sum = 0; | |
double x; | |
while (cin >> x) | |
{ | |
count++; | |
sum += x; | |
} | |
// count가 0일 경우 ArithmeticException 발생 | |
if (count == 0) | |
{ | |
count = 1; // count는 1로 만들어 0.4*sum/count 결과가 0이 되도록 | |
} | |
streamsize precision = cout.precision(); | |
cout << "당신의 최종 점수는 " << setprecision(3) | |
<< 0.2*midtermExam + 0.4*finalExam + 0.4*sum / count | |
<< setprecision(precision) << endl; | |
return 0; | |
} |

개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'C++ > Accelerated C++' 카테고리의 다른 글
Accelerated C++ 4장 연습문제 (5) | 2019.10.08 |
---|---|
Accelerated C++ 2장 연습문제 (0) | 2019.06.23 |