Skip to content

Commit cec0228

Browse files
authored
Create Food_Chain.c
1 parent 10ba273 commit cec0228

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

Codechef Problems/Food_Chain.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* PROBLEM STATEMENT
2+
A great deal of energy is lost as metabolic heat when the organisms from one trophic level are consumed by the next level.
3+
Suppose in Chefland the energy reduces by a factor of K, i.e, if initially, the energy is X, then the transfer of energy to the next tropic level is ⌊XK⌋. This limits the length of foodchain which is defined to be the highest level receiving non-zero energy.
4+
E is the energy at the lowest tropic level. Given E and K for an ecosystem, find the maximum length of foodchain.
5+
Note: ⌊x⌋ denoted the floor function, and it returns the greatest integer that is less than or equal to x (i.e rounds down to the nearest integer). For example, ⌊1.4⌋=1, ⌊5⌋=5, ⌊−1.5⌋=−2, ⌊−3⌋=−3 , ⌊0⌋=0.
6+
Input Format
7+
First line will contain T, number of testcases. Then the testcases follow.
8+
Each testcase contains a single line of input, two integers E,K.
9+
Output Format
10+
For each testcase, output in a single line answer to the problem.
11+
Constraints
12+
1≤T≤104
13+
1≤E≤109
14+
2≤K≤109
15+
Sample Input 1
16+
3
17+
5 3
18+
6 7
19+
10 2
20+
Sample Output 1
21+
2
22+
1
23+
4
24+
Explanation
25+
TestCase 1: The energy at first level is 5 units. For the second level energy becomes ⌊53⌋=1 units. So the length of foodchain is 2 since from the next level onwards 0 units of energy will be received.
26+
TestCase 3: The energy at different levels is:
27+
Level 1- 10 units
28+
Level 2- ⌊102⌋=5 units
29+
Level 3- ⌊52⌋=2 units
30+
Level 4- ⌊22⌋=1 units
31+
Level 5- ⌊12⌋=0 units
32+
So the answer is 4, since it is the last level to receive non-zero energy. */
33+
#include <stdio.h>
34+
35+
int main(void) {
36+
37+
int t,e,k;
38+
int i; int j=0;
39+
scanf("%d",&t);
40+
int tc=t; int arr[t];
41+
while(t--)
42+
{
43+
scanf("%d %d",&e,&k);
44+
int count=0;
45+
i=e;
46+
while(i>0)
47+
{
48+
count++;
49+
i=i/k;
50+
51+
}
52+
arr[j]=count;
53+
j++;
54+
}
55+
56+
for(j=0;j<tc;j++)
57+
printf("%d\n",arr[j]);
58+
return 0;
59+
}

0 commit comments

Comments
 (0)