算法04
Posted luojianyi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了算法04相关的知识,希望对你有一定的参考价值。
题目:有一个矩形数组,第一行是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 }
题目:现在有一个正方形,然后我们现在要将这个正方形上的数原地顺时针旋转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 }
以上是关于算法04的主要内容,如果未能解决你的问题,请参考以下文章
片段(Java) | 机试题+算法思路+考点+代码解析 2023
续:纠正:ubuntu7.04可以安装,而且完美的安装 ! for《Oracle-10.2.0.1,打补丁10.2.0.5:在 debian 版本4不含4以上,及 ubuntu 7.04不含(代码片段