문제 : https://www.acmicpc.net/problem/1431
[ 알고리즘풀이 ]
문제에서 원하는 조건대로 cmp 함수를 작성하여 STL sort를 이용하면 됩니다.
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
bool cmp(string A, string B) {
if (A.size() < B.size())
return true;
else if (A.size() > B.size())
return false;
else {
int sumA = 0, sumB = 0;
for (int i = 0; i < A.size(); i++){
if ('0' <= A[i] && A[i] <= '9')
sumA += A[i] - '0';
if ('0' <= B[i] && B[i] <= '9')
sumB += B[i] - '0';
}
if (sumA < sumB)
return true;
else if (sumA > sumB)
return false;
else {
if (A < B)
return true;
else
return false;
}
}
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int N;
string input;
vector<string> list;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> input;
list.push_back(input);
}
sort(list.begin(), list.end(), cmp);
for (int i = 0; i < N; i++)
cout << list[i] << '\n';
return 0;
}
'Problem Solving > BOJ' 카테고리의 다른 글
[BOJ] 14891 : 톱니바퀴 - travelbeeee (0) | 2020.02.19 |
---|---|
[BOJ] 1500 :최대 곱 -travelbeeee (0) | 2020.02.19 |
[BOJ] 17406 : 배열 돌리기 4 - travelbeeee (0) | 2020.02.18 |
[BOJ] 10971 : 외판원 순회 2 - travelbeeee (0) | 2020.02.17 |
[BOJ] 3085 : BOMBONI - travelbeeee (0) | 2020.02.17 |