A. Vanya and Cubes
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 = 3cubes,
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 test(s)
input
1
output
1
input
25
output
4
Note
Illustration to the second sample:
很简单的一道打表题,也就是怎么样才能推出来递推公式的关键了,主要就是通过总结了,
第一层1,
第二层1+2,
第三层1+2+3,
,,,
第i层 1+2+3+...+i
那么sum = 1+1+2+1+2+3+1+2+..+i+..+1+2+..+n;
得到了第i层所需要的方块数和第i+1层所需要的方块数目之间的关系:
a[i+1] = a[i]+i+1;
然后查表,找出上届就可以了。
代码:
# include<cstdio> # include<iostream> using namespace std; # define MAX 1234 int a[MAX]; void dabiao() { a[1] = 1; for ( int i = 1;i < 1000;i++ ) { a[i+1] = a[i]+i+1; } } int main(void) { dabiao(); int n; while ( cin>>n ) { int ans = 0; int i = 1; while ( n-a[i]>=0 ) { n = n-a[i]; ans = i; i++; } cout<<ans<<endl; } return 0; }