ACM用N个正方体来建造金字塔问可以建造多少层
Description
Vanya got n cubes. He decided to build a pyramid from them. Vanya wants to build the pyramid as follows: the top level of the pyramid must consist of 1 cube, the second level must consist of 1 + 2 = 3 cubes, the third level must have 1 + 2 + 3 = 6 cubes, and so on. Thus, the i-th level of the pyramid must have 1 + 2 + ... + (i - 1) + i cubes.
Vanya wants to know what is the maximum height of the pyramid that he can make using the given cubes.
Input
The first line contains integer n (1 ≤ n ≤ 104) — the number of cubes given to Vanya.
Output
Print the maximum possible height of the pyramid in the single line.
Sample Input
Input
1
Output
1
Input
25
Output
4
Hint
Illustration to the second sample:
解题思路:首先需要一个控制程序结束的语句(也就是要我们所拥有的正方体数大于我们所建楼层所需的总的正方体数)然后需要两个数 t和s(用来分别累加每层需要的正方体数和总共需要的正方体数)最后用一个判断语句判断我们所拥有的正方体数是否可以满足这些楼层的建设;如果不能满足就只能建设完成 h-1层楼的建设。
程序代码:
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int n,h=0,t=0,s=0;
scanf("%d",&n);
while(s<n) //控制程序执行的条件
{
h++;
t=t+h;
s=s+t;
}
if(n<s) //判断我们所拥有的正方体数是否可以满足这些楼层的建设
h=h-1;
printf("%d\n",h);
return 0;
}