【每日一题13】AcWing 754. 平方矩阵 II

Day13 AcWing 754. 平方矩阵 II

思路

  1. 找规律

拓展

  1. AcWing 753. 平方矩阵 I
  2. AcWing 755. 平方矩阵 III
  3. AcWing 727. 菱形
  4. AcWing 748 - 752
  5. AcWing 3154. 打印大X
  6. AcWing 118. 分形

代码

算法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
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 110;

int n;
int a[N][N];

int main()
{
while (cin >> n, n)
{
for (int i = 1; i <= n; i ++ )
{
for (int j = i, k = 1; j <= n; j ++, k ++ )
{
a[i][j] = k;
a[j][i] = k;
}
}

for (int i = 1; i <= n; i ++ )
{
for (int j = 1; j <= n; j ++ )
cout << a[i][j] << ' ';
cout << endl;
}
cout << endl;
}

return 0;
}
算法2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 110;

int n;
int main()
{
while (cin >> n, n)
{
for (int i = 1; i <= n; i ++ )
{
for (int j = i; j >= 1; j -- ) cout << j << ' ';
for (int j = i + 1; j <= n; j ++ ) cout << j - i + 1 << ' ';
cout << endl;
}
cout << endl;
}

return 0;
}
算法3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 110;

int n;
int main()
{
while (cin >> n, n)
{
for (int i = 1; i <= n; i ++ )
{
for (int j = 1; j <= n; j ++ )
cout << abs(i - j) + 1 << ' ';
cout << endl;
}
cout << endl;
}

return 0;
}