-
-
Save wang-nima/03ae1f5d144b281d7412 to your computer and use it in GitHub Desktop.
1. 给一个二维数组,对角线打印 例子: 1 2 3 4.5 6 7 8 9 10 11 12 打印出 1 2 5 3 6 9 4 7 10 8 11 12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=141288&extra=page%3D1%26filter%3Dsortid%26sortid%3D311%26sortid%3D311 | |
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
#include <unordered_map> | |
#include <string> | |
using namespace std; | |
void print(vector<vector<int>> a) { | |
int m = (int)a.size(); | |
if (m == 0) return; | |
int n = (int)a[0].size(); | |
if (n == 0) return; | |
int square = min(m, n); | |
// phase 1 | |
for (int i = 0; i < square; i++) { | |
for (int j = i; j >= 0; j--) { | |
cout << a[i-j][j] << " "; | |
} | |
cout << endl; | |
} | |
if (m >= n) { | |
// phase 2 | |
for (int i = square; i < m; i++) { | |
for (int j = 0; j < square; j++) { | |
cout << a[i-j][j] << " "; | |
} | |
cout << endl; | |
} | |
// phase 3 | |
for (int i = square - 1; i > 0; i--) { | |
for (int j = 0; j < i; j++) { | |
cout << a[m-i+j][n-1-j] << " "; | |
} | |
cout << endl; | |
} | |
} | |
} | |
int main() { | |
vector<vector<int>> v = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}}; | |
print(v); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment