zoukankan      html  css  js  c++  java
  • 2015 HUAS Provincial Select Contest #2~C

    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

    解题思路:这个问题就是判断输入的数的和是在金字塔的第几行,但是要注意的是,如果输入的数不是在这一行的最后一位,那么输出的数字需要减去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; }
  • 相关阅读:
    @Target:注解的作用目标
    Node.js学习笔记(2)
    Node.js学习笔记(1)
    javascript小记-javascript运行机制
    javascript小记-作用域
    javascript小记-闭包理解
    php中ajax跨域请求---小记
    饼状图一
    QPainter使用不同风格的QBrush来填充区域
    QPainter绘制特殊线条
  • 原文地址:https://www.cnblogs.com/chenchunhui/p/4655088.html
Copyright © 2011-2022 走看看