方阵形对角矩阵

谁说胖子不能爱 提交于 2019-12-03 20:36:06

 

DB_Question

 

 

 

 

解决:

  1、存

  2、验证

 1 #include <iostream>
 2 using namespace std;
 3 
 4 //将小矩阵的值转存在一维数组b中
 5 void TurnToB(int a[][16], int b[], int m, int t);
 6 //关键的存放算法
 7 void CoreStore(int b[], int x, int y, int val, int m);
 8 //验证i,j到k是否正确,具体检验k是否能输出
 9 void show(int b[], int i, int j, int m);
10 
11 int main()
12 {
13     int n = 16, m = 4, t = 4;//以16×16方阵为例
14     int a[16][16];
15     int b[64];//m*m*t
16     //初始化特殊位置的值
17     a[0][0] = 0, a[0][3] = 3, a[3][0] = 3;
18     a[3][3] = 6, a[7][4] = 11, a[4][7] = 11;
19     a[7][7] = 14, a[8][11] = 19, a[11][8] = 19;
20     a[11][11] = 22, a[12][15] = 27, a[15][12] = 27;
21     a[15][15] = 30, a[5][5] = 10, a[10][11] = 21;
22     //-------------------------------------------
23     TurnToB(a, b, m, t);
24     //验证上一步算法的正确性
25     show(b, 0, 0, m);
26     show(b, 0, 3, m);
27     show(b, 3, 0, m);
28     show(b, 3, 3, m);
29 
30     show(b, 7, 4, m);
31     show(b, 4, 7, m);
32     show(b, 7, 7, m);
33     show(b, 8, 11, m);
34 
35     show(b, 11, 8, m);
36     show(b, 11, 11, m);
37     show(b, 12, 15, m);
38     show(b, 15, 12, m);
39 
40     show(b, 15, 15, m);
41     show(b, 5, 5, m);
42     show(b, 10, 11, m);
43 
44     return 0;
45 }
46 void TurnToB(int a[][16], int b[], int m, int t)
47 {
48     int x, y;
49     for (int i = 0; i < m; i++)
50     {
51         for (int j = 0; j < m; j++)
52         {
53             for (int k = 0; k < t; k++)
54             {
55                 x = i + m*k;
56                 y = j + m*k;
57                 CoreStore(b, x, y, a[x][y], m);
58             }
59         }
60     }
61 }
62 void CoreStore(int b[], int x, int y, int val, int m)
63 {
64     int loc = (x / m)*m*m + (x%m)*m + y - (x / m)*m;
65     b[loc] = val;
66 }
67 void show(int b[], int i, int j, int m)
68 {
69     int loc = (i / m)*m*m + (i%m)*m + j - (i / m)*m;
70     cout << "a[" << i << "][" << j << "]=b[" << loc << "]=" << b[loc] << endl;
71 }
View Code

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!