반응형

숫자(1~99,999)와 디스플레이 크기(1~10) s를 입력하면 [(2s+3)행 X (s+2)열] 크기의 디지털 숫자로 표시하는 프로그램을 만들어 보자. 각 숫자 사이에는 1줄의 공백이 있다.


숫자 5,238을 s=1의 크기로 표시한 예.


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
85
86
87
88
89
90
91
92
93
94
#include <iostream>
 
using namespace std;
 
int main()
{
    const int MAX_DIGIT = 5;
    const int MAX_SCALE = 10;
 
    char hor_bar[2][MAX_SCALE + 1= { {"          "}, {"----------"} };
    char ver_bar[2][2= { {" "}, {"|"} };
 
    char hor_dig[10][3= { {101}, {000}, {111}, {111}, {010},
        {111}, {111}, {100}, {111}, {111} };    // 0~9
    char ver_dig[10][4= { {1111}, {0101}, {0110}, {0101}, {1101},
        {1001}, {1011}, {0101}, {1111}, {1101} };    // 0~9
    //  -
    // | |
    //  -
    // | |
    //  -
    // 1 digit = 가로선 3개(3줄), 세로선 4개(2줄)
    //             최소 5행 3열
 
    char digits[5][(MAX_SCALE + 3* MAX_DIGIT] = { '\0', };    // 각 숫자 사이에 빈칸 + 마지막 개행 문자 = MAX_SCALE + 3.
                                                                // null문자 '\0'으로 초기화.
    char number[MAX_DIGIT + 1];
    cout << "Enter number(1~99,999): ";
    cin >> number;
 
    int scale;
    cout << "Enter scale(1~10): ";
    cin >> scale;
 
    // scale에 맞게 가로선 길이 조정.
    hor_bar[0][scale] = '\0';
    hor_bar[1][scale] = '\0';
 
    int count_number = strlen(number);
 
    for (int i = 0; i < count_number; i++)
    {
        int real_number = number[i] - '0';    // 문자를 숫자로 변환.
 
        // 두 번째 숫자부터 각 숫자마다 빈 칸 삽입.
        if (i > 0)
            for (int j = 0; j < 5; j++)
                strcat_s(digits[j], sizeof(digits[j]), " ");
 
        // 첫 번째 줄 삽입.
        strcat_s(digits[0], sizeof(digits[0]), " ");
        strcat_s(digits[0], sizeof(digits[0]), hor_bar[hor_dig[real_number][0]]);
        strcat_s(digits[0], sizeof(digits[0]), " ");
 
        // 두 번째 줄 삽입.
        strcat_s(digits[1], sizeof(digits[1]), ver_bar[ver_dig[real_number][0]]);
        strcat_s(digits[1], sizeof(digits[1]), hor_bar[0]);
        strcat_s(digits[1], sizeof(digits[1]), ver_bar[ver_dig[real_number][1]]);
 
        // 세 번째 줄 삽입.
        strcat_s(digits[2], sizeof(digits[2]), " ");
        strcat_s(digits[2], sizeof(digits[2]), hor_bar[hor_dig[real_number][1]]);
        strcat_s(digits[2], sizeof(digits[2]), " ");
 
        // 네 번째 줄 삽입.
        strcat_s(digits[3], sizeof(digits[3]), ver_bar[ver_dig[real_number][2]]);
        strcat_s(digits[3], sizeof(digits[3]), hor_bar[0]);
        strcat_s(digits[3], sizeof(digits[3]), ver_bar[ver_dig[real_number][3]]);
 
        // 다섯 번째 줄 삽입.
        strcat_s(digits[4], sizeof(digits[4]), " ");
        strcat_s(digits[4], sizeof(digits[4]), hor_bar[hor_dig[real_number][2]]);
        strcat_s(digits[4], sizeof(digits[4]), " ");
    }
 
    // 첫 번째 줄 출력
    cout << digits[0<< endl;
 
    // 두 번째 줄 출력
    for (int i = 0; i < scale; i++)
        cout << digits[1<< endl;
 
    // 세 번째 줄 출력
    cout << digits[2<< endl;
 
    // 네 번째 줄 출력
    for (int i = 0; i < scale; i++)
        cout << digits[3<< endl;
 
    // 다섯 번째 줄 출력
    cout << digits[4<< endl;
 
    return 0;
}


잘못된 입력에 대한 처리는 생략되어 있다.


실행 결과.



반응형
Posted by J-sean
:
반응형

Random number generator. It encapsulates the state (currently, a 64-bit integer) and has methods to return scalar random values and to fill arrays with random values. Currently, it supports uniform and Gaussian (normal) distributions. The generator uses Multiply-With-Carry algorithm, introduced by G. Marsaglia (http://en.wikipedia.org/wiki/Multiply-with-carry). Gaussian-distribution random numbers are generated using the Ziggurat algorithm (http://en.wikipedia.org/wiki/Ziggurat_algorithm), introduced by G. Marsaglia and W. W. Tsang.


아래 코드는 OpenCV에서 지원하는 난수 발생기의 사용 방법 입니다.

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
#include <opencv2/opencv.hpp>
 
using namespace std;
using namespace cv;
 
int main(int argc, char** argv)
{
    RNG rng(getTickCount());    // Constructor sets the state to the specified value.
    Mat mat(300400, CV_8SC3);
    
    // Fills arrays with random numbers.
    rng.fill(mat, RNG::UNIFORM, Scalar::all(0), Scalar::all(255));
    
    // Returns uniformly distributed integer random number from [a,b) range
    cout << "RNG::uniform()" << endl;
    cout << "- rng.uniform(0, 255): " << rng.uniform(0255<< endl;
    cout << "- rng.uniform(0.0f, 255.0f): " << rng.uniform(0.0f, 255.0f) << endl;
    cout << "- rng.uniform(0.0, 255.0): " << rng.uniform(0.0255.0<< endl << endl;
 
    // Returns a random integer sampled uniformly from [0, N).
    cout << "RNG::operator()" << endl;
    for (int i = 0; i < 5; i++)
        cout << "- rng(10): " << rng(10<< endl;
 
    // The method updates the state using the MWC algorithm and returns
    // the next 32-bit random number.
    cout << endl << "RNG::next()" << endl;
    for (int i = 0; i < 5; i++)
        cout << "- rng.next(): " << rng.next() << endl;
 
    imshow("RNG", mat);
 
    waitKey(0);
 
    return 0;
}
cs



Matrix filled by RNG::fill().


Random numbers generated by RNG.



반응형
Posted by J-sean
: