* Computer Science/C++

list 짝수, 홀수 분리하기

soicem 2018. 10. 31. 11:27

 랜덤하게 값이 들어가있는 두 리스트의 값들을 even, odd로 나눕니다.  2개의 리스트만 가지고 옮겨야 합니다.

 merge sort(sudo code of top down implementation using list)할 때 방법이 생각나서 그거랑 비슷하게 해봤습니다.  remove할 때 값 날아가는거 주의

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
#include <bits/stdc++.h>
using namespace std;
 
int main(){
    int arr1[7= {4671123509};
    int arr2[5= {2163,8 ,79};
    list <int> l1(arr1, arr1+sizeof(arr1) /sizeof(int));
    list <int> l2(arr2, arr2+sizeof(arr2) /sizeof(int));
    list <int>::iterator ita,it1,it2;
    it1=l1.begin();
    it2=l2.begin();
    while(it1!=l1.end() &&it2!=l2.end()){
        if(*it1 %2 ==1){
            l2.push_back(*it1);
            int a = *it1;
            it1++;
            l1.remove(a);
        }else {
            it1++;
        }
        if(*it2 %2==0){
            l1.push_back(*it2);
            int a = *it2;
            it2++;
            l2.remove(a);
        }else {
            it2++;
        }
    }
}
 
cs


'* Computer Science > C++' 카테고리의 다른 글

data structure homework8 in pnu ds class  (0) 2018.11.01
c++ list splice  (0) 2018.11.01
vector 비교하기  (0) 2018.10.02
c++로 스플릿하기  (0) 2018.09.27
stack with template  (0) 2018.05.28