A. Vanya and Cubes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.