보관함
JUNGOL 기초다지기 15 문자열2
niamdank
2020. 1. 3. 22:42
프로그래밍에서 가장 기초적인 내용을 다루는 기초 다지기입니다.
이번 포스팅은 기초 다지기 중 열다섯 번째인 문자열2 문제를 풀어보도록 하겠습니다.
기초 내용인 만큼 다른 설명없이 문제와 코드만 간단하게 작성하도록 하겠습니다.
602 : 문자열2 - 자가진단1
#include <iostream>
#include <string>
using namespace std;
int main()
{
string strArr[5];
for (int i = 4; i >= 0; --i)
{
cin >> strArr[i];
}
for (int i = 0; i < 5; ++i)
{
cout << strArr[i] << endl;
}
}
603 : 문자열2 - 자가진단2
최근 C#만 하다보니 split하는 방법을 잊어버리고 있었는데 찾아보니 다음과 같이 sstream을 include해서 단어 단위로 나눌 수 있는 것 같습니다.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string str;
getline(cin, str);
string word;
int i = 1;
for (stringstream sts(str); sts >> word; ++i)
{
if (i % 2 == 1)
{
cout << word << endl;
}
}
}
604 : 문자열2 - 자가진단3
#include <iostream>
#include <string>
using namespace std;
int main()
{
string strArr[10];
char c;
for (int i = 0; i < 10; ++i)
{
cin >> strArr[i];
}
cin >> c;
for (int i = 0; i < 10; ++i)
{
if (strArr[i][strArr[i].size() - 1] == c)
{
cout << strArr[i] << endl;
}
}
}
605 : 문자열2 - 자가진단4
C++에서 string을 사용하는 경우 의미가 없는 문제네요.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "Hong Gil Dong";
cout << str << endl;
}
606 : 문자열2 - 자가진단5
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
cin >> str;
str.append("fighting");
cout << str << endl;
}
607 : 문자열2 - 자가진단6
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1, str2;
cin >> str1 >> str2;
str2.replace(0, 2, str1.substr(0, 2), 0);
str2.append(str1.substr(0, 2));
cout << str2 << endl;
}
608 : 문자열2 - 자가진단7
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
cin >> str;
if (str.find('c') != -1)
cout << "Yes" << ' ';
else
cout << "No" << ' ';
if(str.find("ab") != -1)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
609 : 문자열2 - 자가진단8
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1, str2, str3;
cin >> str1 >> str2 >> str3;
if (str1 < str2)
{
if (str1 < str3)
{
cout << str1 << endl;
}
else
{
cout << str3 << endl;
}
}
else
{
if (str2 < str3)
{
cout << str2 << endl;
}
else
{
cout << str3 << endl;
}
}
}
610 : 문자열2 - 자가진단9
#include <iostream>
#include <string>
using namespace std;
int main()
{
string strArr[5];
for (int i = 0; i < 5; ++i)
{
cin >> strArr[i];
}
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 5 - i - 1; ++j)
{
if (strArr[j] < strArr[j + 1])
{
swap(strArr[j], strArr[j + 1]);
}
}
}
for (int i = 0; i < 5; ++i)
{
cout << strArr[i] << endl;
}
}
611 : 문자열2 - 자가진단A
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
cin >> str;
int calcIndex;
char calc[5]{ '+', '-', '*', '/', '%' };
for (int i = 0; i < 5; ++i)
{
calcIndex = str.find(calc[i]);
if (calcIndex != -1)
break;
}
cout.setf(ios::fixed);
cout.precision(2);
cout << stoi(str.substr(0, calcIndex)) * 2 << endl;
cout << stof(str.substr(0, calcIndex)) << endl;
}
612 : 문자열2 - 자가진단B
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str, temp;
for (int i = 0; i < 5; ++i)
{
cin >> temp;
str.append(temp);
}
for (int i = 0; i < str.size(); i += 3)
{
cout << str.substr(i, 3) << endl;
}
}
189 : 문자열2 - 형성평가1
이번 문제는 바로 출력하는 게 아니라 뒤에서부터 출력해야 하기때문에 자료를 저장하기위한 list를 사용했습니다.
#include <iostream>
#include <string>
#include <sstream>
#include <list>
using namespace std;
int main()
{
string str;
getline(cin, str);
list<string> listStr;
string word;
for (stringstream sts(str); sts >> word;)
{
listStr.push_back(word);
}
for (auto var = listStr.crbegin(); var != listStr.crend(); var++)
{
cout << *var << endl;
}
}
190 : 문자열2 - 형성평가2
#include <iostream>
#include <string>
using namespace std;
int main()
{
string strArr[5] = { "flower", "rose", "lily", "daffodil", "azalea" };
char c;
cin >> c;
int count = 0;
for (int i = 0; i < 5; ++i)
{
if (strArr[i].find(c, 1) == 1 || strArr[i].find(c, 1) == 2)
{
cout << strArr[i] << endl;
count++;
}
}
cout << count << endl;
}
191 : 문자열2 - 형성평가3
#include <iostream>
#include <string>
#include <list>
using namespace std;
int main()
{
list<string> listStr;
string temp;
while (true)
{
cin >> temp;
if (temp == "0")
break;
listStr.push_back(temp);
}
cout << listStr.size() << endl;
int count = 1;
for (auto it = listStr.cbegin(); it != listStr.cend(); it++)
{
if (count++ % 2 == 0)
continue;
cout << *it << endl;
}
}
192 : 문자열2 - 형성평가4
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n;
cin >> n;
string* strArr = new string[n];
for (int i = 0; i < n; ++i)
{
cin >> strArr[i];
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n - i - 1; ++j)
{
if (strArr[j] > strArr[j + 1])
swap(strArr[j], strArr[j + 1]);
}
}
for (int i = 0; i < n; ++i)
{
cout << strArr[i] << endl;
}
delete[] strArr;
}
193 : 문자열2 - 형성평가5
#include <iostream>
#include <string>
using namespace std;
int main()
{
string strArr[5];
for (int i = 0; i < 5; ++i)
{
cin >> strArr[i];
}
char c;
string str;
cin >> c >> str;
bool isNone = true;
for (int i = 0; i < 5; ++i)
{
if (strArr[i].find(c) != -1 || strArr[i].find(str) != -1)
{
isNone = false;
cout << strArr[i] << endl;
}
}
if (isNone)
cout << "none" << endl;
}
194 : 문자열2 - 형성평가6
#include <iostream>
#include <string>
using namespace std;
int main()
{
string A, B;
int n;
cin >> A >> B >> n;
A.append(B);
B.replace(0, n, A.substr(0, n));
cout << A << endl;
cout << B << endl;
}
215 : 문자열2 - 형성평가7
#include <iostream>
#include <string>
using namespace std;
bool IsNumber(char c);
int main()
{
string A, B;
cin >> A >> B;
int numA = 0;
for (int i = 0; i < A.size(); ++i)
{
if (!IsNumber(A[i]))
numA = stoi(A.substr(0, i));
}
if (numA == 0)
numA = stoi(A);
int numB = 0;
for (int i = 0; i < B.size(); ++i)
{
if (!IsNumber(B[i]))
numB = stoi(B.substr(0, i));
}
if (numB == 0)
numB = stoi(B);
cout << numA * numB << endl;
}
bool IsNumber(char c)
{
if (c < '0') return false;
if (c > '9') return false;
return true;
}
216 : 문자열2 - 형성평가8
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
while (true)
{
cin >> str;
if (str.compare("END") == 0)
break;
for (auto c = str.crbegin(); c != str.crend(); c++)
{
cout << *c;
}
cout << endl;
}
}
237 : 문자열2 - 형성평가9
이 부분에서는 고정소수점으로 3자리만 사용하기 위해 출력 스트림이 필요합니다.
그런데 화면에 바로 출력하는 게 아닌 string으로 출력해야 하므로 stringstream을 사용하면 됩니다.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int n;
float f;
string str;
cin >> n >> f >> str;
stringstream sts;
sts.setf(ios::fixed);
sts.precision(3);
sts << n << f << str;
string newStr{ sts.str() };
int strSize = newStr.size();
int firstLineSize = (strSize % 2 == 1) ? strSize / 2 + 1 : strSize / 2;
cout << newStr.substr(0, firstLineSize) << endl;
cout << newStr.substr(firstLineSize) << endl;
}
http://www.jungol.co.kr/bbs/board.php?bo_table=pbank&sca=10f0
JUNGOL | 문제은행 1 페이지
경기도 안양시 동안구 평촌대로 109 협성골드프라자 601호 TEL : 031-360-4144 FAX : 031-388-0996 E-mail : hancomc@hotmail.com, comkiwer@naver.com Copyrightⓒ 2010-2019 jungol. All right reserved. TOP
www.jungol.co.kr