开始搬砖!
开始搬砖!
1 #include<stdio.h> 2 #include<iostream> 3 4 bool GetResult(int **& array, unsigned x, unsigned y) 5 { 6 array = new int *[x]; 7 for (unsigned i = 0; i < x; ++i) 8 { 9 array[i] = new int[y]; 10 } 11 12 int t = 0; 13 auto length = x > y ? x : y; 14 for (unsigned i = 0; i < 2 * length - 1; ++i) 15 { 16 for (int j = i; j >= 0; --j) 17 { 18 if (x > (i - j) && y > j) 19 array[i - j][j] = ++t; 20 } 21 } 22 return true; 23 } 24 25 void DeleteResult(int **&array, unsigned x, unsigned y) 26 { 27 for (unsigned i = 0; i < x; ++i) 28 { 29 delete array[i]; 30 } 31 delete array; 32 array = nullptr; 33 } 34 35 int main() 36 { 37 unsigned x, y; 38 std::cin >> x >> y; 39 int ** result = nullptr; 40 if (GetResult(result, x, y)) 41 { 42 for (unsigned i = 0; i < x; ++i) 43 { 44 for (unsigned j = 0; j < y; ++j) 45 { 46 printf("%d ", result[i][j]); 47 } 48 printf("\n"); 49 } 50 DeleteResult(result, x, y); 51 } 52 return 0; 53 }
Hyapp
并不保证完全正确,欢迎指正