https://www.acmicpc.net/problem/25083
25083번 새싹
나의 풀이
#include <iostream>
int main()
{
std::cout << " ,r'\"7\nr`-_ ,' ,/\n \\. \". L_r'\n `~\\/\n |\n |";
}
문자열 사이의 특수 문자들에 유의하여 문자열을 출력한다.
https://www.acmicpc.net/problem/3003
3003번 킹, 퀸, 룩, 비숍, 나이트, 폰
나의 풀이
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// 킹, 퀸, 룩, 비숍, 나이트, 폰 개수
// 1, 1, 2, 2, 2, 8 이 정상
vector<int> v{ 1, 1, 2, 2, 2, 8 };
vector<int> iv;
int a;
while (cin >> a)
iv.push_back(a);
for (int i = 0; i < v.size(); ++i) {
cout << v[i] - iv[i] << ' ';
}
}
원래 필요한 개수를 킹, 퀸, 룩, 비숍, 나이트, 폰 순으로 vector에 저장했다.
그 다음 입력으로 받은 갯수들을 새로운 vector에 저장하고,
원래 vector에서 새로운 vector값을 빼면 올바른 세트가 되기 위해서 필요한 개수를 음수 혹은 양수의 형태로 구할 수 있다.
https://www.acmicpc.net/problem/2444
2444번 별 찍기 - 7
나의 풀이
#include <iostream>
int main()
{
int a;
std::cin >> a;
for (int i = 1; i <= a; ++i) {
for (int k = 0; k < a - i; ++k)
std::cout << ' ';
for (int j = 0; j < 2 * i - 1; ++j)
std::cout << '*';
std::cout << std::endl;
}
for (int i = a - 1; i > 0; --i) {
for (int j = 0; j < a - i; ++j)
std::cout << ' ';
for (int k = 0; k < 2 * i - 1; ++k)
std::cout << '*';
std::cout << std::endl;
}
}
위 방향 삼각형과 아래 방향 삼각형으로 나눠 반복문을 수행하도록 구조화 했다.
가운데 한 줄은 한 번만 출력되어야 하기 때문에 위 방향 삼각형에서 출력하도록 했다.
https://www.acmicpc.net/problem/10988
10988번 팰린드롬인지 확인하기
나의 풀이
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string s1;
cin >> s1;
string s2{ s1 };
reverse(s2.begin(), s2.end());
cout << (s1 == s2);
}
문자열을 거꾸로 했을 때 원래의 문자열과 완전히 일치하면 팰린드롬이라고 한다.
reverse 함수를 이용해서 문자열의 역순을 새로 저장하고 이를 비교하기만 하면 된다.
https://www.acmicpc.net/problem/1157
1157번 단어 공부
나의 풀이
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
string str;
cin >> str;
transform(str.begin(), str.end(), str.begin(), [](char& c) {
return toupper(c);
});
vector<int> v(26);
for (const char& c : str) {
++v[c - 'A'];
}
auto max_first = max_element(v.begin(), v.end());
auto res = find(max_first+1, v.end(), *max_first);
if (res == v.end()) // 여러 개 존재하지 않을 때
cout << static_cast<char>(max_first - v.begin() + 'A');
else
cout << "?";
}
가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램이다.
대문자와 소문자를 구분하지 않기 때문에 transform과 toupper 함수를 이용해서 모든 알파벳을 대문자로 통일했다.
그 다음 각 알파벳의 사용 횟수를 저장하는 vector를 선언하고, 문자열을 순차 접근하여 ++ 연산으로 횟수를 센다.
일단 가장 많이 나온 알파벳을 찾고, 그 사용 횟수와 일치하는 알파벳을 찾는다.
못찾았다면 가장 많이 나온 알파벳을 출력하고, 찾았다면 물음표를 출력한다.
https://www.acmicpc.net/problem/2941
2941번 크로아티아 알파벳
나의 풀이
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<string> v{ "c=", "c-", "d-", "lj", "nj", "s=", "z="};
string str;
cin >> str;
size_t length{ str.length() };
for (const string& elem : v) {
auto res = str.find(elem);
while (string::npos != res) {
length -= (elem.length() - 1);
if (elem == "z=" && str[res - 1] == 'd') {
--length;
}
res = str.find(elem, res + 1);
}
}
cout << length;
}
입력받은 문자열의 길이를 저장한다. 이를 가지고 크로아티아 알파벳이 발견되면 1씩 줄여 처리를 한다.
크로아티아 알파벳 중 'dz=' 와 'z=' 를 구분하기 위해서 'dz='는 따로 저장하지 않고, 'z='를 발견했을 때 그 전의 문자가 'd' 라면 'dz='로 간주하였다. 'dz='일 경우 전체 길이에서 한 번 더 1을 줄인다.
https://www.acmicpc.net/problem/1316
1316번 그룹 단어 체커
나의 풀이
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
// 그룹 단어를 판정하는 방법
// 모든 알파벳에 대해서 find를 했을 때 이전의 find와 2이상 차이난다면 그룹 단어 아님.
int n;
cin >> n;
vector<string> v;
string str;
while (cin >> str) {
v.push_back(str);
}
for (const string& s : v) {
for (char i = 'a'; i <= 'z'; ++i) {
auto res = find(s.begin(), s.end(), i);
bool isGroup{ true };
while (res != s.end()) {
auto before_res = res;
res = find(res + 1, s.end(), i);
// 새로운 itr(res)와 원래 itr(before_res)가 2이상 차이나면 그룹 단어가 아님
if (res == s.end()) {
break;
}
if (res - before_res > 1) {
isGroup = false;
break;
}
}
if (not isGroup) {
--n;
break;
}
}
}
cout << n;
}
나는 find를 2번 해서 얻은 두 반복자가 2 이상 차이나고, 두 번째 반복자가 end()가 아니라면 그룹 단어가 아니고, 이 조건을 만족하지 않는다면 그룹 단어라고 판정했다.
각 문자열에 대해서 모든 알파벳으로 위의 판증 방법을 사용했다.
중간에 그룹 단어가 아님이 판정이 나면 해당 루프를 빠져나가도록 했다.
https://www.acmicpc.net/problem/25206
25206번 너의 평점은
나의 풀이
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <numeric>
using namespace std;
int main()
{
// 입력은 20줄
// 과목명, 학점, 등급
vector<pair<double, double>> v;
unordered_map<string, double> m{
{"A+", 4.5}, {"A0", 4.0}, {"B+", 3.5}, {"B0", 3.0},
{"C+", 2.5}, {"C0", 2.0}, {"D+", 1.5}, {"D0", 1.0}, {"F", 0.0}
};
string obj;
double d;
string score;
for (int i = 0; i < 20; ++i) {
cin >> obj;
cin >> d;
cin >> score;
if (score != "P")
v.push_back(make_pair(d, m[score]));
}
double sum = accumulate(v.begin(), v.end(), static_cast<double>(0), [](double& sum, pair<double, double>& p) {
return sum + (p.first * p.second);
});
double credit = accumulate(v.begin(), v.end(), static_cast<double>(0), [](double& sum, pair<double, double>& p)
{
return sum + p.first;
});
cout.precision(7);
cout << sum / credit;
}
일단 학점과 등급 쌍을 저장한 map을 선언했다. 입력받을 등급을 이용해서 학점을 얻어낼 것이기 때문이다.
과목명, 학점, 등급을 입력받아서 학점과 등급에 따른 학점 쌍을 vector에 저장한다.
vector에 저장된 데이터들을 이용해 전공 평점 평균을 구할 수 있다.
오차가 10의 -4 제곱 이하여야 하기 때문에 cout의 멤버 함수 precision을 사용했다.
** 오류 지적은 환영입니다. ^^ **
'코테' 카테고리의 다른 글
백준 단계별로 풀어보기 (5) - 문자열 (0) | 2024.09.09 |
---|---|
백준 단계별로 풀어보기 (4) (0) | 2024.09.06 |
백준 단계별로 풀어보기 (3) (6) | 2024.09.02 |
백준 단계별로 풀어보기 (2) (0) | 2024.08.31 |
백준 단계별로 풀어보기 (1) (0) | 2024.08.30 |