π§ μ½λ©ν
μ€νΈ
[μΈνλ° C++] 35. Special Sort(κ΅¬κΈ μΈν°λ·°)
peewoong
2024. 5. 2. 15:40
π§ λ¬Έμ
Nκ°μ μ μκ° μ λ ₯λλ©΄ λΉμ μ μ λ ₯λ κ°μ μ λ ¬ν΄μΌ νλ€.
μμ μ μλ μμͺ½μ, μμ μ μλ λ·μͺ½μ μμ΄μΌ ν©λλ€. λν μμ μ μμ μμ μ μμ μμμλ λ³ν¨μ΄ μμ΄μΌ νλ€.
π§ μ λ ₯
첫 λ²μ§Έ μ€μ μ μ Nμ΄ μ£Όμ΄μ§κ³ , κ·Έ λ€μ μ€λΆν° μμλ₯Ό ν¬ν¨ν μ μκ° μ£Όμ΄μ§λ€. μ«μ 0μ μ λ ₯λμ§ μλλ€.
π§ μΆλ ₯
μ λ ¬λ κ²°κ³Όλ₯Ό μΆλ ₯νλ€.
π§ 1νΈ (κ°μ νμ΄)
λ²λΈ μ λ ¬ νμ©
μμμ μμκ° λ§λλ©΄ μμΉλ₯Ό λ³κ²½ν΄μ€λ€. μμκ° μμ μλ κ²½μ°
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
int main(void){
freopen("input.txt", "rt", stdin);
int n, temp;
int a[101];
cin >> n;
for(int i = 0; i < n; ++i){
cin >> a[i];
}
for(int i = 0; i < n-1; ++i){
for(int j = 0; j < n-i-1; ++j){
if(a[j] > 0 && a[j+1] < 0){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for(int i = 0; i < n; ++i){
cout << a[i] << ' ';
}
return 0;
}