Uva11040 Add bricks in the wall

題目:

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.

Paste_Image.png

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

題意:
45塊石頭按照上述方式排列澈歉,每塊石頭上有一個整數。
除了最后一行外,每塊石頭上的整數等于支撐它的兩塊石頭上的正數之和。目前只有奇數行的左數奇數個位置上的數已知,你的任務是求出其余所有整數蚕钦。輸入保證有唯一解(《算法競賽入門經典(第2版)》)。

如圖所示:
z
x+a y+a
x a y
則x+a+y+a=x+y+2*a=z鹅很,則a = (z-x-y) / 2.這樣我們就可以以此計算出奇數行偶數個位置上的數嘶居。之后就可以通過題目中的條件計算出偶數行的數。

參考代碼:

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 10+2;

int mp[N][N];

void init() {
    memset(mp, 0, sizeof(mp));
}

void input() {//奇數行的奇數列已知;
    for (int i = 1;i <= 9;i += 2) {
        for (int j = 1;j <= i;j += 2) {
            cin >> mp[i][j];
        }
    }
}

void bricks() {
    //cout << "xyz" << endl;
    for (int i = 3;i <= 9;i += 2) {//先將單行雙列的數填滿;
        for (int j = 2;j <= i;j += 2) {
            mp[i][j] = (mp[i-2][j-1] - mp[i][j-1] - mp[i][j+1]) / 2;
        }
    }
    //for (int i = 1;i <= 9;++i) {
    //  for (int j = 1;j <= i;++j) {
    //      cout << mp[i][j] << " ";
    //  }
    //  cout << endl;
    //}
    //cout << "xyz" << endl;
    for (int i = 2;i <= 8;i += 2) {//填滿雙行;
        for (int j = 1;j <= i;++j) {
            mp[i][j] = mp[i+1][j] + mp[i+1][j+1];
        }
    }
}

void print() {
    for (int i = 1;i <= 9;++i) {
        int cnt = 0;
        for (int j = 1;j <= i;++j) {
            if (cnt > 0) cout << " ";
            cout << mp[i][j];
            ++cnt;
        }
        cout << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t;
    while (t--) {
        init();
        input();
        bricks();
        print();
    }
    return 0;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末促煮,一起剝皮案震驚了整個濱河市邮屁,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌菠齿,老刑警劉巖佑吝,帶你破解...
    沈念sama閱讀 218,525評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異泞当,居然都是意外死亡迹蛤,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評論 3 395
  • 文/潘曉璐 我一進店門襟士,熙熙樓的掌柜王于貴愁眉苦臉地迎上來盗飒,“玉大人,你說我怎么就攤上這事陋桂∧嫒ぃ” “怎么了?”我有些...
    開封第一講書人閱讀 164,862評論 0 354
  • 文/不壞的土叔 我叫張陵嗜历,是天一觀的道長宣渗。 經常有香客問我抖所,道長,這世上最難降的妖魔是什么痕囱? 我笑而不...
    開封第一講書人閱讀 58,728評論 1 294
  • 正文 為了忘掉前任田轧,我火速辦了婚禮,結果婚禮上鞍恢,老公的妹妹穿的比我還像新娘傻粘。我一直安慰自己,他們只是感情好帮掉,可當我...
    茶點故事閱讀 67,743評論 6 392
  • 文/花漫 我一把揭開白布弦悉。 她就那樣靜靜地躺著,像睡著了一般蟆炊。 火紅的嫁衣襯著肌膚如雪稽莉。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,590評論 1 305
  • 那天涩搓,我揣著相機與錄音污秆,去河邊找鬼。 笑死缩膝,一個胖子當著我的面吹牛混狠,可吹牛的內容都是我干的。 我是一名探鬼主播疾层,決...
    沈念sama閱讀 40,330評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼将饺,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了痛黎?” 一聲冷哼從身側響起予弧,我...
    開封第一講書人閱讀 39,244評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎湖饱,沒想到半個月后掖蛤,有當地人在樹林里發(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,693評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡井厌,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,885評論 3 336
  • 正文 我和宋清朗相戀三年蚓庭,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片仅仆。...
    茶點故事閱讀 40,001評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡器赞,死狀恐怖,靈堂內的尸體忽然破棺而出墓拜,到底是詐尸還是另有隱情港柜,我是刑警寧澤,帶...
    沈念sama閱讀 35,723評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站夏醉,受9級特大地震影響爽锥,放射性物質發(fā)生泄漏。R本人自食惡果不足惜畔柔,卻給世界環(huán)境...
    茶點故事閱讀 41,343評論 3 330
  • 文/蒙蒙 一氯夷、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧靶擦,春花似錦肠槽、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽嘴拢。三九已至桩盲,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間席吴,已是汗流浹背赌结。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留孝冒,地道東北人柬姚。 一個月前我還...
    沈念sama閱讀 48,191評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像庄涡,于是被迫代替她去往敵國和親量承。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,955評論 2 355

推薦閱讀更多精彩內容