There is a two-dimensional pyramid of bricks. The weight of the 1st - 1 kg, crushes evenly for 2 below it at 0.5 kg. That is, if the upper one presses +100 kg, then below it each will be (100 + 1) / 2 kg - the lower one presses the weight of each one above it.

1,0 = .5 2,0 = .75 2,1 = 1.5 3,0 = .875 3,1 = .2.125

How to calculate the pressure on any of them, indicating the position, for example, k4-2, k8-4 ... k (row Above, number Left) - as a function with 2 parameters, which returns the weight?

I thought, a number of * .5 + (number of 1) / 2 or 1 * (c + 1) / 2 * p + 1 * c / 2 * p, various other options, but I just can not find the exact one. (preferably on java)

  • "pick up"? - sum of geometric and arithmetic progressions - Igor
  • The question is certainly not entirely clear. It is necessary to try to guess what it is about. In fact, for the pyramid the loads will not be uniform, since for such structures the load on each element depends on how far it is from the center. But if we assume that the load is uniform, then this problem is solved easily. The output format k4-2 is also unclear, ... Here 4 is a row on top, and 2 is a load on a brick? - AR Hovsepyan
  • @CodeGust, maybe this is a false idea, but the digits in your pyramid are quite symmetrical and look like Pascal's triangle, I'm not good at math, but maybe you should try to solve the problem not through recursion, but through binomial coefficients, I'm not strong at this topic , but you can dig deeper - goldstar_labs

2 answers 2

And why not use recursion? Weight values, row number and brick number in the row, insert what you need:

public static void main(String[] args) { float weight = 1; //вСс ΠΊΠΈΡ€ΠΏΡ‡Π° int row = 5; //Π½ΠΎΠΌΠ΅Ρ€ ряда начиная с 1 с Π²Π΅Ρ€Ρ…Ρƒ int num = 3; //Π½ΠΎΠΌΠ΅Ρ€ ΠΊΠΈΡ€ΠΏΠΈΡ‡Π° слСва (ΠΈΠ»ΠΈ с ΠΏΡ€Π°Π²Π°, ΠΊΠ°ΠΊ большС нравится) с 1 System.out.println(pressueOnBrick(row, num, weight)); } //Π΄Π°Π²Π»Π΅Π½ΠΈΠ΅ Π½Π° ΠΎΠ΄ΠΈΠ½ ΠΊΠΈΡ€ΠΏΠΈΡ‡ public static float pressueOnBrick(int row, int num, float weight) { return pressue(row, num, weight) - weight; //Π΄Π°Π²Π»Π΅Π½ΠΈΠ΅ ΠΎΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌΠΎΠ΅ Π½Π° ΠΎΠ΄ΠΈΠ½ ΠΊΠΈΡ€ΠΏΠΈΡ‡ = ΠΏΠΎΠ»Π½ΠΎΠ΅ //Π΄Π°Π²Π»Π΅Π½ΠΈΠ΅ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ ΠΎΠΊΠ°Π·Ρ‹Π²Π°Π΅Ρ‚ ΠΊΠΈΡ€ΠΏΠΈΡ‡ минус вСс ΠΊΠΈΡ€ΠΏΠΈΡ‡Π° } //ΠΏΠΎΠ»Π½ΠΎΠ΅ Π΄Π°Π²Π»Π΅Π½ΠΈΠ΅ ΠΎΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌΠΎΠ΅ ΠΊΠΈΡ€ΠΏΠΈΡ‡Ρ‘ΠΌ (Π²ΠΊΠ»ΡŽΡ‡Π°Ρ свой вСс) public static float pressue(int row, int num, float weight) { //Π² ряду Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ ΠΊΠΈΡ€ΠΏΠΈΡ‡Π΅ΠΉ с Π½ΠΎΠΌΠ΅Ρ€ΠΎΠΌ мСньшС 1 ΠΈ большС Ρ‡Π΅ΠΌ Π½ΠΎΠΌΠ΅Ρ€ ряда(число ΠΊΠΈΡ€ΠΏΠΈΡ‡Π΅ΠΉ Π² ряду = Π½ΠΎΠΌΠ΅Ρ€Ρƒ ряда) if (num < 1 || num > row) { return 0; } //Π΄Π°Π²Π»Π΅Π½ΠΈΠ΅ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ ΠΎΠΊΠ°Π·Ρ‹Π²Π°Π΅Ρ‚ ΠΊΠΈΡ€ΠΏΠΈΡ‡ Ρ€Π°Π²Π½ΠΎ суммС вСса ΠΊΠΈΡ€ΠΏΠΈΡ‡Π° ΠΈ ΠΏΠΎΠ»ΠΎΠ²ΠΈΠ½Π΅ вСса ΠΊΠΈΡ€ΠΏΠΈΡ‡Π΅ΠΉ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ давят Π½Π° Π½Π΅Π³ΠΎ //слСва ΠΈ справа return weight + (pressue(row - 1, num - 1, weight) + pressue(row - 1, num, weight)) / 2; } 

Validation of input values ​​on your conscience.

  • Because it is an algorithm of exponential complexity. At a minimum, you need to save the previously calculated weights into an array, and re-use them when you re-query ... we get O (n ^ 2), although you can also O (1). - Chorkov
  • What does this have to do with the problem statement? - Yevgeny Glushkov
  • reference values: 1.0 = .5 2.0 = .75 2.1 = 1.5 3.0 = .875 3.1 = .2.125 - CodeGust
  • I do not know what is your coordinate system in the pyramid, but if I take the top row of one brick as 1 and further down where 2 is the second row, the 3rd and the third and so on and count in the row with 1 brick, it turns out: 1-1 = 0 (top brick, does not press anything on it), 2-1 = 0.5, 3-1 = 0.75, 4-1 = 0.875, 4-2 = 2.125, 25-14 = 20.660532. If I understand correctly, then you have the numbering of rows and positions of bricks in a row starting at 0, the given recursive solution is easy to adapt to this. - Yevgeny Glushkov
  • exactly thank you Yes, from the beginning there should be. only here 323,157 (= 306.487 ...) pushes execution. - CodeGust
 #include <iostream> #include <map> int main() { unsigned ranks{}; std::cin >>ranks; // ΠΎΠ±Ρ‰Π΅Π΅ количСство рядов double weight{}; std::map<unsigned, double> loads; unsigned k{1}; for (; k <= ranks; ++k) { loads.insert(std::make_pair(k, weight)); weight = (weight + 1)/2; } std::cout << std::endl << "Π²Π²ΠΎΠ΄ΠΈΠΌ Π½ΡƒΠΆΠ½Ρ‹ΠΉ ряд: " << std::endl; std::cin >> k; std::cout << "Π½Π°Π³Ρ€ΡƒΠ·ΠΊΠ° для ΠΊΠΈΡ€ΠΏΠΈΡ‡Π΅ΠΉ " << k << " - Π³ΠΎ ряда: " << loads[k]; return 0; } 

After discussing with goldstar_labs to rewrite, because the first option is true for two rows and for extreme bricks.

 #include <iostream> #include <vector> using std::vector; using std::cin; void setValue(const vector<double>& p1, vector<double>& p2) { size_t k = p2.size(); if (k <= 2) return; for (size_t i = 1; i < k - 1; ++i) p2[i] += (p1[i] + 1)/2; } int main() { unsigned ranks{}, k{}; cin >>ranks; // ΠΎΠ±Ρ‰Π΅Π΅ количСство рядов double w{}; vector< vector<double> > weights; for (unsigned n = 1; n <= ranks; ++n) { vector<double> v(n, w); weights.emplace_back(v); if (n > 2 ) setValue(weights[n - 2], weights[n - 1]); w = (w + 1)/2; } std::cout << std::endl << "Π²Π²ΠΎΠ΄ΠΈΠΌ Π½ΡƒΠΆΠ½Ρ‹ΠΉ ряд: " << std::endl; std::cin >> k; k %= ranks + 1; std::cout << "Π½Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ для ΠΊΠΈΡ€ΠΏΠΈΡ‡Π΅ΠΉ " << k << " - Π³ΠΎ ряда:\n"; for (double d : weights[k - 1]) std::cout << d << ' '; return 0; } 

Output, I think you can do it yourself in the desired format.

  • This is a partial solution that will load the outer layer of bricks in the pyramid. In the inner layers, the pressure will be greater, because one brick of the inner layer will crush two bricks on top - goldstar_labs
  • @goldstar_labs, I wrote about this in a comment, but the question says about uniform load, so the answer is appropriate - AR Hovsepyan
  • The question is muddy, I agree) on the one hand, evenly, on the other hand, "1.0 = .5 2,0 = .75 2,1 = 1.5 3,0 = .875 3,1 = .2.125", probably still uneven . For uneven, it looks like a Pascal triangle, where there is an incomprehensible coefficient on each line, I'll go and think about it) - goldstar_labs
  • @goldstar_labs, maybe it was meant that the pressure is evenly distributed to the next two bricks. In any case, figured out, and thank you - AR Hovsepyan