반응형
1181 단어 정렬
https://www.acmicpc.net/problem/1181
Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define MAX 20000+10
int N;
string tmp;
vector <string> vec;
void Input(){
cin >> N;
for(int i=0;i<N;i++){
cin >> tmp;
vec.push_back(tmp);
}
}
void Output(){
tmp = "";
for(int i=0;i<N;i++)
if(tmp == vec[i]) continue;
else{
cout << vec[i] << "\n";
tmp = vec[i];
}
}
bool comp(string a, string b){
if(a.length() == b.length()) return a < b;
else return a.length() < b.length();
}
void Sort(){
sort(vec.begin(), vec.end(), comp);
}
int main(void){
Input();
Sort();
Output();
return 0;
}
1431 시리얼 번호
https://www.acmicpc.net/problem/1431
Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define MAX 1000+10
int N;
vector <string> vec;
void Input(){
string tmp="";
cin >> N;
for(int i=0;i<N;i++){
cin >> tmp;
vec.push_back(tmp);
}
}
void Output(){
for(int i=0;i<N;i++)
cout << vec[i] << "\n";
}
int cal(string word){
int sum=0;
// 숫자만 뽑아내기
for(int i=0;i<word.length();i++){
if('0' <= word[i] && word[i] <= '9'){
sum += word[i] - '0';
}
}
return sum;
}
bool comp(string a, string b){
int sum_a, sum_b = 0;
// A와 B의 길이가 다르면 짧은 것이 먼저온다
if(a.length() != b.length()) return a.length() < b.length();
// 서로 길이가 같다면 A의 모든 자리수의 합과 B의 모든 자리수의 합을 비교해서 작은 합을 가지는 것이 먼저 온다. (숫자인 것만 더한다)
else{
sum_a = cal(a);
sum_b = cal(b);
if(sum_a != sum_b) return sum_a < sum_b;
else{
// 만약 1, 2번 둘 조건으로도 비교할 수 없으면, 사전순으로 비교한다 숫자가 알파벳보다 사전순으로 작다.
return a < b;
}
}
}
void Solution(){
sort(vec.begin(), vec.end(), comp);
}
int main(void){
Input();
Solution();
Output();
}
10989 수 정렬하기 3
https://www.acmicpc.net/problem/10989
Code
#include <iostream>
using namespace std;
int N;
int tmp;
int count_arr[10000+10];
int main(void){
ios::sync_with_stdio(0); cin.tie(0);
cin >> N;
for(int i=0;i<N;i++){
cin >> tmp;
count_arr[tmp]++;
}
for(int i=1;i<10001;i++){
while(count_arr[i]){
cout << i << "\n";
count_arr[i]--;
}
}
return 0;
}
반응형
'Algorithm' 카테고리의 다른 글
[Algorithm] 큐(Queue) (0) | 2023.04.09 |
---|---|
[Algorithm] 스택(Stack) (0) | 2023.04.09 |
[Algorithm] 계수 정렬(Counting Sort) (0) | 2023.04.09 |
[Algorithm] 힙 정렬(Heap Sort) (0) | 2023.04.09 |
[Algorithm] C++ STL sort 함수 (0) | 2023.04.08 |