[Daily Blue Bridge] 34. 2016 Provincial Competition Java Group Real Question "Number of Briquettes"

Hello, I am the little gray ape, a programmer who can write bugs!

Welcome everyone to pay attention to my column " Daily Blue Bridge ". The main function of this column is to share with you the real questions of the Blue Bridge Cup provincial competitions and finals in recent years, analyze the algorithm ideas, data structures and other content that exist in it, and help you learn To more knowledge and technology!

Title: Number of Briquettes

There is a pile of briquettes piled into a triangular pyramid prism, specifically:

Put 1 on the first layer,

Put 3 on the second layer (stacked into a triangle)

Put 6 on the second layer (stacked into a triangle)

Put 10 on the second layer (stacked into a triangle)

......

If there are 100 layers, how many briquettes are there?

Please fill in the number representing the total number of briquettes,

Note: What you submit should be an integer, don’t fill in any extra or descriptive text

Problem-solving ideas:

This question is regular. When we understand the question, we can find that the number of triangles in each layer is equal to the number of layers, and the number of briquettes in the last row is also equal to the number of layers, as follows:

It can be seen from this that we only need to add the number of briquettes in the previous layer to the number of the cap layer to get the number of briquettes in this layer, and finally use the cyclic addition to get the total.

 

Answer source code:

public class Year2016_Bt1 {

	public static void main(String[] args) {
		int ans = 0;
		int sum = 0;
		int i = 1;
		while (i<=100) {
			sum+=i;
			ans+=sum;
			i++;
		}
		System.out.println(ans);
	}

}

 

Sample output:

 

There are deficiencies or improvements, and I hope that my friends will leave a message and learn together!

Interested friends can follow the column!

Little Gray Ape will accompany you to make progress together!

 

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/115004216