안선생의 개발 블로그
C++ 메트릭스 본문
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
|
#include <iostream>
using namespace std;
class LowerTri { // 하부 삼각 행렬
int* A;
int n; //열수
public:
LowerTri()
{
n = 2;
A = new int[2 * (2 + 1) / 2]; //대각 아래 삼각형
}
LowerTri(int n)
{
this->n = n;
A = new int[n *( n + 1) / 2]; // 대각 아래 삼각형
}
~LowerTri()
{
delete[] A;
}
public:
void Set(int i, int j, int x);
int Get(int i, int j);
void Display();
};
void LowerTri::Set(int i, int j, int x)
{
if (i >= j)
A[i * (i - 1) / 2 + j - 1] = x; // 행 계산(행)
//A[n *(j-1)-(j-2)*(j-1)/2+i-j] = x; // 열 계산(열)
}
int LowerTri::Get(int i, int j)
{
if (i == j)
return A[i * (i - 1) / 2 + j - 1];
//return A[n *(j-1)-(j-2)*(j-1)/2+i-j] = x;
else
return 0;
}
void LowerTri::Display()
{
int i, j;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
if (i >= j)
cout << A[i * (i - 1) / 2 + j - 1] << " ";
//cout << //A[n *(j-1)-(j-2)*(j-1)/2+i-j] = x;
else
cout << "0 ";
}
cout << "\n";
}
}
int main()
{
int input;
cout << "길이를 입력";
cin >> input;
LowerTri m(input);
int x;
for (int i = 1; i <= input; i++)
{
for (int j = 1; j <= input; j++)
{
cin >> x;
m.Set(i, j, x);
}
}
cout << "\n";
m.Display();
return 0;
};
|
cs |