CodeForces - 492A(第三期白银组比赛T2)

Time limit
1000 ms
Memory limit
262144 kB

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
Input
1
Output
1
Input
25
Output
4

问题分析:
要计算层数则要计算每一层个数,每一层个数由上一层推出,则运用规律即可计算每层石头个数,从而判断。

代码分析:
在函数内计算每一层石头个数,再进行判断。

#include 
using namespace std;
int ff(int x)//调用函数
{
	int m = 1, k, i, p[50];
	p[1] = 1;
	for (i = 2; i < 50; i++)//把每一层石头个数存入数组中
	{
		p[i] = p[i - 1] + i;
		cout << p[i] << " ";
	}
	k = p[1];
	while (k < x)//计算层数
	{
		k += p[m + 1];
		if (k > x)
			break;
		m++;
	}
	return m;
}
int main()
{
	int n;
	cin >> n;
	cout << ff(n) << endl;
}

你可能感兴趣的:(CodeForces - 492A(第三期白银组比赛T2))