Click here to Skip to main content
15,887,240 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Samodelkin, Professor decided make by hand volumetric model bricks from matches, and use the matches for edges. Length of edges of every brick equally for one match.
For model’s construction of three bricks, he uses 28 matches.
What least quantity of matches Samodelkin needs for construction of model with N bricks?

Every number of problems does not exceed 2·10^9.

Input data: One number N is quantity of bricks.

Output data: One number is quantity of matches.
Example Input example #1
N=1 

Output example #1
12

link :Match’s model - Problems - Eolymp[^]

What I have tried:

return 12+(n-1)*8;
where n is the number of bricks and n>=1;
Posted
Updated 14-Sep-23 9:41am
v4
Comments
jeron1 15-Aug-23 10:57am    
It looks like they want to calculate the answer to

12+(n-1)*8

in a function and return that calculated value from the function, using C++ or Java, based on your tags.

What it is saying is that a brick is a cube, and has sides one match long.
So a single brick requires 12 matches.
If you put two bricks together, they can share matches: one needs 12, the other needs 4 less.
For three or more bricks, each end needs 12 matches, and each non-end brick needs only 4.

So what you have to do is write a function that takes a number of bricks N and returns the number of matches needed to make it: 12 for N == 1, 12 + 8 for N == 2, and 12 + 4 * (N-2) + 12 for values of N greater than 2.

Give it a try: it's not a complex problem. If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Go back to the question and look at the picture. From that you can see that a brick at the end of a block requires 12 matches, and one inside the block requires 4. So from that you can easily calculate the number of matches required for any number of bricks.
 
Share this answer
 
v2
As jeron1 has already commented, according to the sketch, blocks of 12 matchsticks are obviously assembled at the edges.
While you need all 12 matchsticks for the first block, you need only 8 for each further block.
The result should not exceed 2* 10^9, which means that you get along with 31 bits. Since the number should also not be negative, an unsigned int with 32 bits would be suitable.

C++
uint32_t n,x;
//...
x = 12 + (n-1)*8; // with n min 1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900