码迷,mamicode.com
首页 > 其他好文 > 详细

习题10-1 砌砖 UVa11040

时间:2015-02-27 10:12:59      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:uva   递推关系   

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;
}

习题10-1 砌砖 UVa11040

标签:uva   递推关系   

原文地址:http://blog.csdn.net/u014800748/article/details/43957395

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!