티스토리 뷰

반응형


#. Problem

* The copyright in this matter is in Inflearn


#. Resolution Process

  1. Read and understand problem

  2. Redefine the problem + abstract

  3. Create solution plan (select Algorithm, Data structure)

  4. Prove the plan (check performance time and usage memory)

  5. Carry out the plan

  6. Look back on the plan and find a way to improve it


#. Solve

  1. 두 배열의 원소를 하나씩 비교하면서 출력하면 될 것 같다.

     아래와 같이 두 배열이 입력되었을 때,

     1 3 5

     2 3 6 7 9


     1 < 2 이므로 1 출력

     p1 = 1, p2 = 0


     1 3 5

     2 3 6 7 9

     3 > 2 이므로 2 출력

     p = 1, p2 = 1


     1 3 5

     2 3 6 7 9

     3 = 3 이므로 3 출력

     p1 = 2, p2 = 1


     1 3 5

     2 3 6 7 9

     5 < 3 이므로 3 출력

     p1 = 2, p2 = 2


     1 3 5

     2 3 6 7 9

     5 < 6 이므로 5 출력

     p1 = 3, p2 = 2


     p1는 index를 초과하였으므로, 

     두 번째 배열에서 나머지 2, 3, 4 번째 index 를 출력해주면 될 것 같다.


#. Code

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <cstdio>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int main(void)
{
    freopen("input.txt""rt", stdin);
 
    int n1, n2, i, p1 = 0, p2 = 0;
 
    scanf("%d"&n1);
    vector<int> v1(n1);
 
    for (i = 0; i < n1; i++)
    {
        scanf("%d"&v1[i]);
    }
 
    scanf("%d"&n2);
    vector<int> v2(n2);
 
    for (i = 0; i < n2; i++)
    {
        scanf("%d"&v2[i]);
    }
 
    while (p1 < n1 && p2 < n2)
    {
        if (v1[p1] <= v2[p2])
        {
            printf("%d ", v1[p1]);
            p1++;
        }
        else
        {
            printf("%d ", v2[p2]);
            p2++;
        }
    }
 
    if (p1 < n1)
    {
        for (i = p1; i < n1; i++)
        {
            printf("%d ", v1[i]);
        }
    }
    
    if (p2 < n2)
    {
        for (i = p2; i < n2; i++)
        {
            printf("%d ", v2[i]);
        }
    }
 
    return 0;
}
cs


아래 코드는 달라진게 별로 없어보이지만

강사님의 깔끔한 코드 스타일을 적용해서 아주 조금.. 수정해본 코드이다

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
32
33
34
35
36
37
#include <cstdio>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int main(void)
{
    freopen("input.txt""rt", stdin);
 
    int n1, n2, i, p1 = 0, p2 = 0;
 
    scanf("%d"&n1);
    vector<int> v1(n1);
    for (i = 0; i < n1; i++)
        scanf("%d"&v1[i]);
 
    scanf("%d"&n2);
    vector<int> v2(n2);
    for (i = 0; i < n2; i++)
        scanf("%d"&v2[i]);
 
    while (p1 < n1 && p2 < n2)
    {
        if (v1[p1] <= v2[p2])
            printf("%d ", v1[p1++]);
        else
            printf("%d ", v2[p2++]);
    }
 
    while(p1 < n1) 
        printf("%d ", v1[p1++]);
    while (p2 < n2) 
        printf("%d ", v2[p2++]);
 
    return 0;
}
cs

우선 (line 26, 28) 에서 증가연산 ++ 를 따로 하지 않고 배열참조 후 수행하도록 하였고,

(line 31 ~ 34) 도 if 와 for문 콜라보대신 while 문을 사용하였고, 마찬가지로 증가연산 ++ 를 따로 하지 않고 배열참조 후 수행하도록 하였다.

훨씬 이전보다 코드가 깔끔해졌다..!


#. Other code

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
32
33
34
35
36
37
38
#include<stdio.h>
#include<vector>
#include<algorithm>
 
using namespace std;
 
int a[101], b[101], c[300];
 
int main(){
    int n, m, i, p1=1, p2=1, p3=1;
 
    scanf("%d"&n);
    for(i=1; i<=n; i++){
        scanf("%d"&a[i]);
    }
 
    scanf("%d"&m);
    for(i=1; i<=m; i++){
        scanf("%d"&b[i]);
    }
 
    while(p1<=&& p2<=m){
        if(a[p1]<b[p2]){
            c[p3++]=a[p1++];
        }
        else{
            c[p3++]=b[p2++];
        }
    }
 
    while(p1<=n) c[p3++]=a[p1++];
    while(p2<=m) c[p3++]=b[p2++];
 
    for(i=1; i<p3; i++)
        printf("%d ", c[i]);
    
    return 0;
}
cs

다행이 로직은 같게 구현하였다. Yeah~!

다만 강사님의 코드가 훨씬 깔끔해보이는건 기분탓인 것일까..

더 노력해야지ㅠㅠ


#. Result

  - Input --------------------------------------------------------

3

1 3 5

5

2 3 6 7 9

------------------------------------------------------------------


  - Output --------------------------------------------------------

1 2 3 3 5 6 7 9

------------------------------------------------------------------



반응형

'PS > Problem_Solving' 카테고리의 다른 글

[Inflearn] 뮤직비디오(binary search apply)  (0) 2020.04.27
[Inflearn] 연속된 자연수의 합  (0) 2020.04.26
[Inflearn] Inversion Sequene  (0) 2020.04.24
[Inflearn] 3등의 성적은?  (0) 2020.04.23
[Inflearn] 탄화수소 질량  (0) 2020.04.23
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday