码迷,mamicode.com
首页 > 其他好文 > 详细

ACM用N个正方体来建造金字塔问可以建造多少层

时间:2015-07-17 17:53:08      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

ACM用N个正方体来建造金字塔问可以建造多少层

标签:

原文地址:http://www.cnblogs.com/xinxiangqing/p/4654814.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!