1.题目描述:点击打开链接
2.解题思路:找规律题。通过观察易得到如下递推式:(a[i][j]表示第i层,第j个位置的砖的数字)
(1) a[i+2][j+1]=(a[i][j]-a[i+2][j]-a[i+2][j+2])/2;
(2) a[i+1][j]=a[i+2][j+1]+a[i+2][j];
(3) a[i+1][j+1]=a[i+2][j+1]+a[i+2][j+2];
3.代码:
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<algorithm> #include<string> #include<sstream> #include<set> #include<vector> #include<stack> #include<map> #include<queue> #include<deque> #include<cstdlib> #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #include<functional> using namespace std; #define N 9 int a[N][N]; int main() { //freopen("test.txt", "r", stdin); int t; cin >> t; while (t--) { memset(a, 0, sizeof(a)); for (int i = 0; i < 9; i+=2) for (int j = 0; j < i + 1; j += 2) cin >> a[i][j]; for (int i = 6; i >= 0; i-=2) { for (int j = 0; j < i + 1; j += 2) { a[i + 2][j + 1] = (a[i][j] - a[i + 2][j] - a[i + 2][j + 2]) / 2; a[i + 1][j] = a[i + 2][j + 1] + a[i + 2][j]; a[i + 1][j + 1] = a[i + 2][j + 1] + a[i + 2][j + 2]; } } for (int i = 0; i < 9;i++) for (int j = 0; j < i + 1; j++) printf("%d%c", a[i][j], j == i ? '\n' : ' '); } return 0; }
时间: 2024-10-27 01:21:51