Title Link: Http://codeforces.com/problemset/problem/492/A
Main topic:
Use n cubes to build a pyramid, the first horizontal line uses 1 cubes, the second horizontal line uses (1+2) a cube, the third horizontal line uses (1+2+3) a cube, and so on, the first cube uses (1+2+3+...+i) a cube, the total number of cubes used is not more than N, Q What is the maximum number of layers of pyramids created by n cubes? (pyramids are shown below)
Topic Analysis:
You can consider the number of cubes required to build a pyramid of layer I, and use a loop statement such as a for statement to calculate. After the first layer of the building comparison, more than the number of cubic meters can not continue to stack, jump out of the loop, if not, then continue the next round of the cycle.
Source:
1#include <iostream>2 using namespacestd;3 intMain ()4 {5 intn,m=0, k=0, count=0;//n is the test data6 while(cin>>N) {7 for(intI=1; i<=n;i++)8 { 9M+=i;//calculate the number of cubic meters required by the pyramid layer ITenCount+=m;//calculate the total number of cubes needed to build an I-level pyramid Onek++;//number of pyramid layers A if(count==N) - Break;//Create an I-layer pyramid cube number exactly equal to the number of cubes provided - Else if(count>N) the{k--;//building an I-layer pyramid cube exceeds the number of cubes provided - Break; - } - } +cout<<k<<Endl; -k=0; +m=0; ACount=0; at } - return 0; -}
A. Vanya and Cubes