第二题C++ #include<iostream> (5488)#include<vector> using namespace std; int main() { int n; cin >> n; vector<int>fib(n*n, 0); fib[0] = 1; fib[1] = 1; for (int i = 2; i < n*n; i++) { fib[i] = fib[i - 1] + fib[i - 2]; } int res = fib.size() - 1; int left = 0, right = n - 1, top = 0, bottom = n - 1; //top、bottom横坐标 ; left、right纵坐标 vector<vector<int>>vec(n,vector<int>(n,0)); while (1) { for (int i = left; i <= right; ++i) { vec[top][i] = fib[res]; --res; } if (++top > bottom) break; for (int j = top; j <= bottom; ++j) { vec[j][right] = fib[res]; --res; } if (--right < left) break; for (int m = right; m >= left; --m) { vec[bottom][m] = fib[res]; --res; } if (--bottom < top) break; for (int n = bottom; n >= top; --n) { vec[n][left] = fib[res]; res--; } if (++left > right) break; } for (auto n : vec) { vector<int>temp = n; for (auto m : temp) cout << m << " "; cout << endl; } return 0; }
点赞 评论

相关推荐

牛客网
牛客网在线编程
牛客网题解
牛客企业服务