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 | 31 |
Tags
- 자바
- 힙정렬
- 기수정렬
- 삽입정렬
- 퀵정렬
- 정렬
- 자료구조
- 다이나믹프로그래밍
- 프로그래밍
- Median of Medians
- java
- 계수정렬
- 동적프로그래밍
- 병합정렬
- 알고리즘
- SNS
- C++
- 정수론
- DP
- 선택정렬
- 수학
- 백트래킹
- 동적계획법
- 프로그래밍언어
- 재귀
- 선택알고리즘
- 안드로이드
- 백준
- 코딩테스트
- 버블정렬
Archives
- Today
- Total
MODE::CREATIVE
[백준][c++] 1431번: 시리얼 번호 본문
https://www.acmicpc.net/problem/1431
문제 해석
- 문제에 제시된 조건으로 문자열들을 정렬한다
알고리즘 분류
- 정렬
풀이
- C++ STL인 sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp) 함수에 문제에 제시된 비교조건을 구현하여 인자로 사용한다
코드
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
bool cmp(const string &o1, const string &o2) {
if (o1.length() != o2.length()) {
return o1.length() < o2.length();
}
else {
int o1Sum = 0, o2Sum = 0;
for (char c : o1) {
if(isdigit(c)) o1Sum += c - '0';
}
for (char c : o2) {
if(isdigit(c)) o2Sum += c - '0';
}
if (o1Sum != o2Sum) return o1Sum < o2Sum;
else return o1 < o2;
}
}
int main() {
int n;
cin >> n;
vector<string> serials(n);
for (int i=0; i<n; i++) {
cin >> serials[i];
}
sort(serials.begin(), serials.end(), cmp);
// 정렬된 결과 출력
for (const string &serial : serials) {
cout << serial << endl;
}
return 0;
}
'BOJ' 카테고리의 다른 글
[백준][c++] 1463번: 1로 만들기 (0) | 2024.10.06 |
---|---|
[백준][c++] 1735번: 분수 합 (2) | 2024.10.03 |
[백준][c++] 1783번: 병든 나이트 (1) | 2024.10.03 |
[백준][c++] 1929번: 소수 구하기 (1) | 2024.10.02 |
[백준][c++] 1966번: 프린터 큐 (0) | 2024.10.02 |