Time
limit:1000MS
Memory Limit:262144KB
64bit IO Format:%i64d &%i6 4u
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 cubes, the second level MU St consist of 1 + 2 = 3 cubes, the third level must has 1 + 2 + 3 = 6 cubes, and so on. Thus, the I-th level of the pyramid must has 1 + 2 + ... + (i -1) + I cubes.< /c15>
Vanya wants to know, the maximum height of the pyramid, the he can make using the given cubes.
Input
The first line contains an integer n (1≤ n ≤104)-the number of cubes given to Vanya.
Output
Print the maximum possible height of the pyramid in the
Sample Input
Input
1
Output
1
Input
25
Output
4
Program Analysis: The test point of this problem is still accumulated. The first time is 1, the second is 1+2, the third is 1+ (1+2) +3 so cycle, so this also requires that we should put each of the answers in a number, for this topic we use an array to solve the problem, in a for loop to add the number to add in. But it should be noted that at the end of our sum-n>0 we were adding more than N, so J should be minus 1. But if sum==0, then the value of J is the value we want to get.
Program code:
#include<cstdio>#include<iostream>UsingNamespace Std;IntMain(){int a[200]={0};int INJ, Sum=0; a[1]=1;For(I=2; I<200; I++){For(j=0; j<=i; j+ +) A[I]+=j;}scanf("%d", &n);For(j=1; j<=i; j++){Sum+=a[j];If(Sum>n){Printf("%d\n ",j-1< Span class= "Sh-symbol"); break;} else if (Sum== N {printf ( "%d\n" ,j< Span class= "Sh-symbol"); break;} }return 0;< Span class= "Sh-cbracket" >
ACM second race (C)