算法04

2021-07-11 12:05

阅读:684

标签:sed   alt   分享   r++   style   spl   pre   class   iostream   

题目:有一个矩形数组,第一行是1,2,3,4....,第二行是在第一行的末尾的数又开始逐渐加1,然后我们要回形打印这个数组

技术分享图片技术分享图片
 1 #include 2 using namespace std;
 3 int arry[100][100];
 4 int col, row;
 5 void func2(int tR, int tC, int dR, int dC)
 6 {
 7     if (tR == dR)
 8     {
 9         for (int i = tC; i )
10         {
11             cout " ";
12         }
13     }
14     else if (tC == dC)
15     {
16         for (int i = tR; i )
17         {
18             cout " ";
19         }
20     }
21     else
22     {
23         int curC = tC;
24         int curR = tR;
25         while (curC != dC)
26         {
27             cout " ";
28             curC++;
29         }
30         while (curR != dR)
31         {
32             cout " ";
33             curR++;
34         }
35         while (curC != tC)
36         {
37             cout " ";
38             curC--;
39         }
40         while (curR != tR)
41         {
42             cout " ";
43             curR--;
44         }
45     }
46 }
47 void func1(int col, int row)
48 {
49     int tR = 0;
50     int tC = 0;
51     int dR = row;
52     int dC = col;
53     while (tR  dC)
54     {
55         func2(tR++, tC++, dR--, dC--);
56     }
57 }
58 int main()
59 {
60     printf("请输入行和列:");
61     cin >> row;
62     cin >> col;
63     int count_num = 0;
64     for (int i = 0; i )
65     {
66         for (int j = 0; j )
67         {
68             arry[i][j] = count_num;
69             count_num++;
70         }
71     }
72     for (int i = 0; i )
73     {
74         for (int j = 0; j )
75         {
76             cout " ";
77         }
78         cout  endl;
79     }
80     func1(--col, --row);
81     return 0;
82 }
View Code

 

题目:现在有一个正方形,然后我们现在要将这个正方形上的数原地顺时针旋转90度,原地旋转不能借助于另一个数组

技术分享图片技术分享图片
 1 #include 2 using namespace std;
 3 int arry[100][100];
 4 int col, row;
 5 void func2(int tR, int tC, int dR, int dC)
 6 {
 7     int tmp = 0;
 8     int item = dC - tC;
 9     for (int i = 0; i != item;i++)
10     {
11         tmp = arry[tR][tC + i];
12         arry[tR][tC + i] = arry[dR - i][tC];
13         arry[dR - i][tC] = arry[dR][dC - i];
14         arry[dR][dC - i] = arry[tR + i][dC];
15         arry[tR + i][dC] = tmp;
16     }
17 }
18 void func1(int col, int row)
19 {
20     int tR = 0;
21     int tC = 0;
22     int dR = row - 1;
23     int dC = col - 1;
24     while (tR  dR)
25     {
26         func2(tR++, tC++, dR--, dC--);
27     }
28 }
29 int main()
30 {
31     printf("请输入行和列:");
32     cin >> row;
33     cin >> col;
34     int count_num = 0;
35     for (int i = 0; i )
36     {
37         for (int j = 0; j )
38         {
39             arry[i][j] = count_num;
40             count_num++;
41         }
42     }
43     func1(col, row);
44     for (int i = 0; i )
45     {
46         for (int j = 0; j )
47         {
48             cout " ";
49         }
50         cout  endl;
51     }
52     return 0;
53 }
View Code

 

算法04

标签:sed   alt   分享   r++   style   spl   pre   class   iostream   

原文地址:https://www.cnblogs.com/luojianyi/p/9551371.html


评论


亲,登录后才可以留言!