Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- Unreal Engine 5
- 알고리즘
- DFS
- 누적 합
- 정렬
- 트리
- 자료구조
- 다익스트라
- 유니온 파인드
- 시뮬레이션
- 브루트포스
- 우선순위 큐
- 다이나믹 프로그래밍
- Team Fortress 2
- c++
- 그리디 알고리즘
- 구현
- XR Interaction Toolkit
- 백준
- 그래프
- 문자열
- VR
- BFS
- 유니티
- 백트래킹
- 투 포인터
- 스택
- 재귀
- 수학
- ue5
Archives
- Today
- Total
1일1알
백준 1431번 시리얼 번호 C++ 본문
https://www.acmicpc.net/problem/1431
MyStr 구조체에 비교연산자 재정의
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
struct MyStr {
string str;
bool operator<(const MyStr& other) const {
if (str.length() == other.str.length()) {
int mySum = 0;
int otherSum = 0;
for (int i = 0; i < str.length(); i++) {
if (str[i] >= '0' && str[i] <= '9') {
mySum += str[i] - '0';
}
if (other.str[i] >= '0' && other.str[i] <= '9') {
otherSum += other.str[i] - '0';
}
}
if (mySum == otherSum) {
return str < other.str;
}
return mySum < otherSum;
}
return str.length() < other.str.length();
}
};
vector<MyStr> v;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string str;
cin >> str;
v.push_back({str});
}
sort(v.begin(), v.end());
for (auto a : v) {
cout << a.str << "\n";
}
};
'알고리즘' 카테고리의 다른 글
백준 3187번 양치기 꿍 C++ (0) | 2023.06.29 |
---|---|
백준 16507번 어두운 건 무서워 C++ (0) | 2023.06.22 |
백준 12101번 1, 2, 3 더하기 2 C++ (0) | 2023.06.10 |
백준 17610번 양팔저울 C++ (0) | 2023.06.09 |
백준 21278번 호석이 두 마리 치킨 C++ (1) | 2023.06.08 |