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
1
1
25
4
解题思路:这个问题就是判断输入的数的和是在金字塔的第几行,但是要注意的是,如果输入的数不是在这一行的最后一位,那么输出的数字需要减去1,如果是最后一位则不需要减去1;在第二次输入数字时,注意和的清零,还有计算次数的数应赋值为1,否则将出现错误。若输入的数字为0,则应该推出程序。
程序代码:
#include<cstdio>
int main()
{
int n,count,sum;
while(scanf("%d",&n)==1&&n)
{
count=1;
sum=0;
for(int i=1;i<=count;i++)
{
sum=sum+((1+i)*i)/2;
if(sum>=n)
{
if(sum==n)
{
printf("%d
",count);
break;
}
else
{
printf("%d
",count-1);
break;
}
}
count++;
}
}
return 0;
}