반응형


cin과 cout은 c언어 다음 단계로 넘어온 코더들에게는 매우 편리한 도구이다.


scanf("%d",&n);이라고 해야될 걸 cin >> n;이라고 하면 끝나기 때문이다.


cout도 마찬가지다.


cout << a << " " << b << " " << a + b << endl;이라는 코드가 편하지


printf("%d %d %d\n",a, b, a + b);는 형식도 복잡하고, 처음 접하는 분들에겐 다소 난해하지 않을수가 없다.



이처럼 편하게 이용할 수 있는 만큼 지불해야 하는 대가가 있다.


바로 시간이다.


printf, scanf는 조금 더 많이 써야하고 형식을 갖추어 주지 않으면 제대로 동작을 하지 않기 마련이지만, 


그 형식을 미리 다 맞추어줌으로써 모든것이 빠르게 해결된다.


하지만, cout, cin은 형식을 모르기 때문에 컴퓨터가 직접 형식을 찾아내어 아 이게 정수이구나, 문자이구나를 판별해야 한다.


따라서 시간이 다소 많이 걸린다.



아래 두 코드를 보자.


두 코드는 cin, cout과 scanf, printf를 빼고 모두 같은 코드이다.


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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
 
using namespace std;
 
int map[301][301];
int DP[301][301];
 
int main()
{
    int y, x;
 
    scanf("%d%d"&y, &x);
 
    for (int i = 0; i < y; i++)
        for (int j = 0; j < x; j++)
            scanf("%d"&map[i][j]);
 
    DP[0][0= map[0][0];
 
    for (int i = 1; i < y; i++)
        DP[i][0= map[i][0];
 
 
    for (int i = 0; i < y; i++)
    {
        for (int j = 1; j < x; j++)
        {
            DP[i][j] = DP[i][j - 1+ map[i][j];
        }
    }
 
    for (int i = 1; i < y; i++)
    {
        for (int j = 0; j < x; j++)
        {
            DP[i][j] = DP[i-1][j] + DP[i][j];
        }
    }
 
    int tCase;
 
    scanf("%d"&tCase);
 
    while (tCase--)
    {
        int y1, x1, y2, x2;
 
        scanf("%d%d%d%d"&y1, &x1, &y2, &x2);
 
        y1--, x1--;
        y2--, x2--;
 
        int ans1, ans2, ans3, ans4;
 
        if (y2 < || x2 < 0)
            ans1 = 0;
 
        else
            ans1 = DP[y2][x2];
 
        if (y1 - < || x2 < 0)
            ans2 = 0;
 
        else
            ans2 = DP[y1 - 1][x2];
 
        if (y2 < || x1 - < 0)
            ans3 = 0;
 
        else
            ans3 = DP[y2][x1 - 1];
 
        if (y1 - < || x1 - < 0)
            ans4 = 0;
 
        else
            ans4 = DP[y1 - 1][x1 - 1];
            
        printf("%d\n", ans1 - ans2 - ans3 + ans4);
    }
    
}
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus





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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
 
using namespace std;
 
int map[301][301];
int DP[301][301];
 
int main()
{
    int y, x;
 
    cin >> y >> x;
 
    for(int i = ; i < y ; i++)
        for (int j = 0; j < x; j++)
            cin >> map[i][j];
 
    DP[0][0= map[0][0];
 
    for (int i = 1; i < y; i++)
        DP[i][0= map[i][0];
 
    for (int i = 0; i < y; i++)
    {
        for (int j = 1; j < x; j++)
        {
            DP[i][j] = DP[i][j - 1+ map[i][j];
        }
    }
 
    for (int i = 1; i < y; i++)
    {
        for (int j = 0; j < x; j++)
        {
            DP[i][j] = DP[i-1][j] + DP[i][j];
        }
    }
 
    int tCase;
 
    cin >> tCase;
 
    while (tCase--)
    {
        int y1, x1, y2, x2;
 
        cin >> y1 >> x1 >> y2 >> x2;
 
        y1--, x1--;
        y2--, x2--;
 
        int ans1, ans2, ans3, ans4;
 
        if (y2 < || x2 < 0)
            ans1 = 0;
 
        else
            ans1 = DP[y2][x2];
 
        if (y1 - < || x2 < 0)
            ans2 = 0;
 
        else
            ans2 = DP[y1 - 1][x2];
 
        if (y2 < || x1 - < 0)
            ans3 = 0;
 
        else
            ans3 = DP[y2][x1 - 1];
 
        if (y1 - < || x1 - < 0)
            ans4 = 0;
 
        else
            ans4 = DP[y1 - 1][x1 - 1];
            
        cout << ans1 -ans2 - ans3 +ans4 << endl;
    }
    
}
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus





하지만 두 코드를 가지고 하나의 문제를 풀어보게 되면, 속도차이가 어마어마하게 난다.



https://www.acmicpc.net/problem/2167 직접 여기에서 채점을 해 보면 알 수 있다.


물론 예를든 문제이지만, 이렇게 시간이 대략 20배가 차이가 나는데 빅데이터를 다루는 과정에서 이러한 


입, 출력이 필요했다면 얼마나 많은 시간이 차이가 날지 의문스럽다.



따라서 cin, cout은 숏코딩 혹은, 간단한 프로그램에서만 이용하고, 


무거운 프로그램일수록 c의 문법을 지향하는것이 더 좋아 보인다.


반응형