码迷,mamicode.com
首页 > 编程语言 > 详细

算法04

时间:2018-08-28 23:51:15      阅读:246      评论:0      收藏:0      [点我收藏+]

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

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

技术分享图片
 1 #include<iostream>
 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 <= dC; i++)
10         {
11             cout << arry[tR][i] << " ";
12         }
13     }
14     else if (tC == dC)
15     {
16         for (int i = tR; i <= dR; i++)
17         {
18             cout << arry[i][tC] << " ";
19         }
20     }
21     else
22     {
23         int curC = tC;
24         int curR = tR;
25         while (curC != dC)
26         {
27             cout << arry[tR][curC] << " ";
28             curC++;
29         }
30         while (curR != dR)
31         {
32             cout << arry[curR][dC] << " ";
33             curR++;
34         }
35         while (curC != tC)
36         {
37             cout << arry[dR][curC] << " ";
38             curC--;
39         }
40         while (curR != tR)
41         {
42             cout << arry[curR][tC] << " ";
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 <= dR && tC <= 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 < row; i++)
65     {
66         for (int j = 0; j < col; j++)
67         {
68             arry[i][j] = count_num;
69             count_num++;
70         }
71     }
72     for (int i = 0; i < row; i++)
73     {
74         for (int j = 0; j < col; j++)
75         {
76             cout << arry[i][j] << " ";
77         }
78         cout << endl;
79     }
80     func1(--col, --row);
81     return 0;
82 }
View Code

 

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

技术分享图片
 1 #include<iostream>
 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 < row; i++)
36     {
37         for (int j = 0; j < col; j++)
38         {
39             arry[i][j] = count_num;
40             count_num++;
41         }
42     }
43     func1(col, row);
44     for (int i = 0; i < row; i++)
45     {
46         for (int j = 0; j < col; j++)
47         {
48             cout << arry[i][j] << " ";
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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!