알고리즘 문제풀이/백준
[백준 2870] 수학숙제
홍시홍
2020. 3. 20. 23:18
요구사항
문자열 안에서 숫자를 뽑아내어 오름차순으로 정열하여라
풀이
stoi를 이용한다면 문자열 길이가 100이라면 어마어마하게 큰 숫자이기 때문에 안된다
단어 중간중간을 string으로 뽑아내어 조건에 따라 예외처리 해주고
string을 정렬하여야 한다
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
using namespace std;
int n;
int map[51];
int target;
bool com(string a, string b) {
if (a.size() < b.size())
return true;
else if (a.size() == b.size()) {
if (a < b) return true;
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
vector<string> v;
for (int i = 0; i < n; i++) {
string str;
string str1;
int flag = 0;
cin >> str;
for (int j = 0; j < str.size(); j++) {
char ch = str[j];
if (((int)ch >= 48) && ((int)ch <= 57)) {
str1 += ch;
}
else {
if (str1.size() > 0) {
v.push_back(str1);
}
str1.clear();
}
}
v.push_back(str1);
}
vector<string> ans;
for (int i = 0; i < v.size(); i++) {
int flag = 0;
string ansstr;
if (v[i] == "") continue;
for (int j = 0; j < v[i].size(); j++) {
if (flag == 0) {
if (v[i][j] == '0') {
continue;
continue;
}
else {
flag = 1;
ansstr += v[i][j];
}
}
else {
ansstr += v[i][j];
}
}
if (flag == 0) {
if (ansstr.size() == 0) {
char ch = '0';
ansstr += ch;
ans.push_back(ansstr);
continue;
}
}
ans.push_back(ansstr);
}
sort(ans.begin(), ans.end(), com);
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << endl;
}
cout << endl;
}