Vanya and Cubes
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
Examples
inputCopy
1
outputCopy
1
inputCopy
25
outputCopy
4
Note
Illustration to the second sample:
问题链接:http://codeforces.com/problemset/problem/492/A。
问题简述:金字塔型,第一层是1,第二层是1+2,第三层是1+2+3……第n层是1+2+3+……+n。给出一个数字n,计算n最多可以叠的层数。
问题分析:利用函数计算第1层到第i层所需的数量sum。将sum与n比较,直到sum等于n,输出层数,或者sum大于n,输出层数-1。
程序说明:使用了函数,并且循环判断。
AC通过的C语言程序如下:
#include <iostream>
using namespace std;
int k, j, i, n, m;
int sum = 0;
int a(int i)
{
for(m = 1;m<=i;m++)
{
sum += m;
}
return sum;
}
int main()
{
cin >> n;
j = 1;
for (i = 1; i <= j; i++)
{
a(i);
if (sum == n)
{
cout << i << endl;
return 0;
}
else if (sum > n)
{
cout << i - 1 << endl;
return 0;
}
else
{
j++;
}
}
}