Skip to content

Commit 4ff7005

Browse files
authored
Create Food_Chain.c
1 parent 004775b commit 4ff7005

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
4+
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.
5+
6+
E is the energy at the lowest tropic level. Given E and K for an ecosystem, find the maximum length of foodchain.
7+
8+
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.
9+
10+
Input Format
11+
First line will contain T, number of testcases. Then the testcases follow.
12+
Each testcase contains a single line of input, two integers E,K.
13+
Output Format
14+
For each testcase, output in a single line answer to the problem.
15+
16+
Constraints
17+
1≤T≤104
18+
1≤E≤109
19+
2≤K≤109
20+
Sample Input 1
21+
3
22+
5 3
23+
6 7
24+
10 2
25+
Sample Output 1
26+
2
27+
1
28+
4
29+
Explanation
30+
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.
31+
32+
TestCase 3: The energy at different levels is:
33+
34+
Level 1- 10 units
35+
Level 2- ⌊102⌋=5 units
36+
Level 3- ⌊52⌋=2 units
37+
Level 4- ⌊22⌋=1 units
38+
Level 5- ⌊12⌋=0 units
39+
So the answer is 4, since it is the last level to receive non-zero energy. */
40+
#include <stdio.h>
41+
42+
int main(void) {
43+
44+
int t,e,k;
45+
int i; int j=0;
46+
scanf("%d",&t);
47+
int tc=t; int arr[t];
48+
while(t--)
49+
{
50+
scanf("%d %d",&e,&k);
51+
int count=0;
52+
i=e;
53+
while(i>0)
54+
{
55+
count++;
56+
i=i/k;
57+
58+
}
59+
arr[j]=count;
60+
j++;
61+
}
62+
63+
for(j=0;j<tc;j++)
64+
printf("%d\n",arr[j]);
65+
return 0;
66+
}

0 commit comments

Comments
 (0)