카테고리 없음
35. Special Sort (버블정렬 응용: 구글 인터뷰) [정렬 & 이분탐색(결정알고리즘) & 투포인트 알고리즘 & 스택]
코다람쥐
2022. 4. 8. 09:47
나의정답.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int main(int argc, char** argv) {
//freopen("input.txt", "rt", stdin);
int n, tmp;
scanf("%d", &n);
vector<int> a(n);
for(int i = 0 ; i < n; i++)
scanf("%d", &a[i]);
for(int i = 0; i < n - 1; i++){
for(int j = 0; j < n - 1 - i; j++){
if( (a[j] * -1 > 0 && a[j+1] > 0) || (a[j] * -1 < 0 && a[j+1] < 0 ) )
{
if(a[j+1] < 0 ){
tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
}
}
for(int i = 0; i < n; i++)
printf("%d ", a[i]);
}
35번은 34번의 응용문제라서 34번은 제외