二维数组旋转45度
//#pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <cstring> #include <algorithm> #include <string> #include <set> #include <functional> #include <numeric> #include <sstream> #include <stack> #include <map> #include <queue> #define CL(arr, val) memset(arr, val, sizeof(arr)) #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define pi acos(-1.0) #define ll long long #define L(x) (x) << 1 #define R(x) (x) << 1 | 1 #define MID(l, r) (l + r) >> 1 #define Min(x, y) (x) < (y) ? (x) : (y) #define Max(x, y) (x) < (y) ? (y) : (x) #define E(x) (1 << (x)) #define iabs(x) (x) < 0 ? -(x) : (x) #define OUT(x) printf("%I64d\n", x) #define keyTree (chd[chd[root][1]][0]) #define Read() freopen("din.txt", "r", stdin) #define Write() freopen("dout.txt", "w", stdout); #define M 100007 #define N 107 using namespace std; int dx[4]={-1,1,0,0}; int dy[4]={0,0,-1,1}; const int inf = 0x7f7f7f7f; const int mod = 1000000007; const double eps = 1e-8; const int R = 100007; int a[N][N]; vector<int> b[2*N]; int main() { int cnt = 0; int n ; cin >> n; for (int i = 0; i < 2*n - 1; ++i) b[i].clear(); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { a[i][j] = cnt++; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { printf("%d ",a[i][j]); } printf("\n"); } for (int i = 0; i < n; ++i) { for (int j = 0; j <= i; ++j) { //b[i][j] = a[j][i - j]; b[i].push_back(a[j][i - j]); } } //输出三角的旋转 // for (int i = 0; i < n; ++i) // { // for (int j = 0; j <= i; ++j) // { // printf("%d ",b[i][j]); // } // printf("\n"); // } for (int i = n,rnum = n - 1; i < 2*n - 1; ++i,rnum--) { for (int j = 0; j < rnum; j++) { //b[i][j] = a[i - n + 1 + j][n - 1 - j]; b[i].push_back(a[i - n + 1 + j][n - 1 - j]); } } printf(">>>>>>\n"); for (int i = 0; i < 2*n - 1; ++i) { for (size_t j = 0; j < b[i].size(); ++j) { printf("%02d ",b[i][j]); } printf("\n"); } }
还是这个比较简单:
for (int i = 0; i < 2*n - 1; ++i) { for (int j = 0; j < n; ++j) { if (i - j < 0 || i - j >= n) continue; b[i].push_back(a[j][i - j]); } }