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塊石頭按照上述方式排列,每塊石頭上有一個(gè)整數(shù)。
除了最后一行外,每塊石頭上的整數(shù)等于支撐它的兩塊石頭上的正數(shù)之和。目前只有奇數(shù)行的左數(shù)奇數(shù)個(gè)位置上的數(shù)已知,你的任務(wù)是求出其余所有整數(shù)。輸入保證有唯一解(《算法競(jìng)賽入門(mén)經(jīng)典(第2版)》)。

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

參考代碼:

#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() {//奇數(shù)行的奇數(shù)列已知;
    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) {//先將單行雙列的數(shù)填滿;
        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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容