This in not “another brick in the wall”, it’s just a matter of adding numbers. Suppose you have a wall with the shape of a triangle, like the one shown below. The wall has 9 rows and row i has exactly i bricks, considering that top row is the first one and bottom row is the ninth. Some bricks are labeled with a number and other ones are blank. Notice that labeled bricks appear only on odd rows and they occupy odd positions within the row. The problem you must solve is finding a suitable number for each blank brick taking into account one simple rule: the number of a brick is obtained by adding the numbers of the two bricks below it. Obviously, this rule does not apply to the ninth row. Numbers are supposed to be integers.

Input

The first line of the input contains an integer N, indicating the number of test cases. This line is followed by the lines corresponding to the test cases. Each test case is described in five lines. These five lines correspond to odd rows of the wall, from top to bottom, as described above. Line i contains the numbers corresponding to odd bricks on row i of the wall (that is, non blank bricks), enumerated from left to right and separated with a single space. It is supposed that each test case is correct, that is, there exists a solution to the problem that the case describes.

Output

For each test case, the output should consist of nine lines describing the numbers of all bricks of the wall. So, line i should contain the numbers corresponding to the i bricks on row i of the wall, enumerated from left to right and separated by a single space.

Note: Here we have an example with two test cases. The first one corresponds to the wall depicted above.

Sample Input

2

255

54 67

10 18 13

3 3 5 2

2 1 2 1 1

256

64 64

16 16 16

4 4 4 4

1 1 1 1 1

Sample Output

255

121 134

54 67 67

23 31 36 31

10 13 18 18 13

5 5 8 10 8 5

3 2 3 5 5 3 2

2 1 1 2 3 2 1 1

2 0 1 0 2 1 1 0 1

256

128 128

64 64 64

32 32 32 32

16 16 16 16 16

8 8 8 8 8 8

4 4 4 4 4 4 4

2 2 2 2 2 2 2 2

1 1 1 1 1 1 1 1 1

问题链接:UVA11040 Add bricks in the wall

问题简述:(略)

问题分析

这个问题首先是数据表示问题。三角形是不方便的,只好用二维数组表示,并且左对齐,将近一半就浪费了。这也是没有办法的。

然后就是找规律,观察以下的三角形数据结构关系:

[c]

[a+x] [x+b]

[a]  [x]  [b]

其中a,b和c是已知的,x是未知的,那么c=(a+x)+(x+b),得x=(c-a-b)/2。

用上述的x公式,就可以计算下标为偶数(第0行不用算)的行的未知项。然后再计算奇数行的各项。

程序说明:(略)

题记:(略)

参考链接:(略)

AC的C++语言程序如下:

/* UVA11040 Add bricks in the wall */#include <bits/stdc++.h>//#define DEBUGusing namespace std;const int N = 9;
int a[N][N];void print()
{for(int i=0; i<N; i++) {for(int j=0; j<=i; j++) {if(j)printf(" ");printf("%d", a[i][j]);}printf("\n");}
}int main()
{int t;#ifdef DEBUGmemset(a, 0, sizeof(a));
#endifscanf("%d", &t);while(t--) {// 读入数据for(int i=0; i<N; i+=2)for(int j=0; j<=i; j+=2)scanf("%d", &a[i][j]);#ifdef DEBUGprint();
#endif// 计算偶数行for(int i=N-1; i>0; i-=2)for(int j=1; j<i; j+=2)a[i][j] = (a[i - 2][j - 1] - a[i][j - 1] - a[i][j + 1]) / 2;#ifdef DEBUGprint();
#endif// 计算奇数行for(int i=1; i<N; i+=2)for(int j=0; j<=i; j++)a[i][j] = a[i+1][j] + a[i+1][j+1];print();}return 0;
}

UVA11040 Add bricks in the wall【数学】相关推荐

  1. Competitive Programming 3题解

    题目一览: Competitive Programming 3: The New Lower Bound of Programming Contests(1) Competitive Programm ...

  2. ICPC程序设计题解书籍系列之一:刘汝佳:《算法竞赛入门经典》(第2版)

    题是书中的题,部分解法参照了书中的解法,不少解法都做了简化和改进. 做程序,就要努力做到自己的程序是最好的! 第3章 数组和字符串(例题) POJ1488 UVA272 UVALive5381 TEX ...

  3. 紫书《算法竞赛入门经典》

    紫书<算法竞赛入门经典>题目一览 第3章 数组和字符串(例题) UVA 272 TEX Quotes UVA 10082 WERTYU UVA 401 Palindromes UVA 34 ...

  4. π-Algorithmist分类题目(3)

    原题网站:Algorithmist,http://www.algorithmist.com/index.php/Main_Page π-Algorithmist分类题目(3) Probability ...

  5. 乐高ev3涉及到的一些赛事_使您成为英雄的前五名开发者技能(提示:涉及LEGO)

    乐高ev3涉及到的一些赛事 Programming is like building something with LEGOs. Any developer can pick up a brand n ...

  6. Java解惑 电子书

    --表达式谜题 Java 谜题 1--表达式谜题 谜题 1:奇数性 下面的方法意图确定它那唯一的参数是否是一个奇数.这个方法能够正确运转 吗? public static boolean isOdd( ...

  7. html导出excel时换行符,ASP.NET 导出到Excel时保留换行的代码

    完整代码: protected void Button1_Click(object sender, EventArgs e) { System.Web.HttpContext curContext = ...

  8. 翻译下 golang package time

    # 关于 `package time` 个人体会:"wall clock" 可以理解为就是实际的时钟,而 "monotonic clock" 则是程序内部的时钟 ...

  9. JDK8特性--Stream(list转map)

    JDK8特性--Stream(求和,过滤,排序)   这里是其他一些stream的有用法,有需要的可以看看. 言归正传,现在主要是是学习list转map,使用的方法是Stream中Collectors ...

最新文章

  1. 从技术上解读大数据的应用现状和开源未来
  2. mysql 存储过程 动态建表_MySQL存储过程动态创建表,数据分表
  3. VC编写的程序不能在其他机器上运行的解决方案(续)
  4. mysql集群多管理节点_项目进阶 之 集群环境搭建(三)多管理节点MySQL集群
  5. java10 WeakHashMap
  6. python中面向对象空间时间_零基础老男孩学Python|面向对象之类的空间问题
  7. 使用VS2010编译64的Geos库
  8. python系统学习:第二周之字典应用
  9. 如何调试一个无法重现的错误?
  10. 安全测试SQL注入与XSS攻击
  11. 公交车管理系统C语言
  12. 产品读书《产品经理的第二本书》
  13. 微信小程序(脱敏处理,通过身份证判断性别,时间段的倒计时)
  14. 线性插值法(一次插值多项式)的Python程序
  15. matlab列联表的独立性检验,(8.4列联表独立性分析案例.ppt
  16. dubbo源码分析-dubbo-serialization
  17. android 360开机启动,手机360设置开机启动项
  18. 鸿蒙系统概述(HarmonyOS)学习这一篇就够了!
  19. LINUX系统基础——文件系统和目录
  20. 简单了解计算机编码知识-(中文编码)

热门文章

  1. 高级着色语言HLSL入门(4)
  2. C#链接各种数据库代码总结
  3. mac mysql php_Mac下搭建PHP开发环境(Apache+PHP+MySQL+phpMyAdmin)
  4. 力扣-图解算法数据结构-剑指 Offer 05. 替换空格
  5. Spark Conf配置用法
  6. Python 之 函数进阶
  7. spring配置数据源错误记录
  8. java 实例域_Java实例域初始化
  9. 下载的JAVA9怎么没有jshell_java9系列(一)安装及jshell使用
  10. 获取公司的maven库 和 idea maven 中Projects Settings的Libraries中正常,但是在Maven Projects中Dependencies一直出现红线的解决办法