Question

In: Computer Science

Stairs Jumping One day, Jojo takes a vacation to the mountains to get away from the...

Stairs Jumping

One day, Jojo takes a vacation to the mountains to get away from the cities. While climbing the mountain, Jojo came to an area filled with thousands of stairs to get to the top. However, there was a rule in the mountain which stated that Jojo had to jump over the stairs with same height to get to the top of the mountain. Listening to these rules, Jojo wanted to know what is the minimum required height he should jump and the minimum number of jumps to reach the top of the mountain. Note: Jojo is on the first stair and the top of the mountain is on the last stair. Jojo can jump several stairs as long as the difference of the height of the steps does not exceed the height of Jojo’s jump.

Format Input:

There are T test cases. Each testcase contains an integer N which represents numbers of stairs that Jojo had to jump. On the next line there are N numbers where each number represents the height of the stair.

Format Output Output:

T line with format “Case # X: Y Z”, where X represents the testcase number, Y represents minimal height Jojo has to jump, and Z represents minimal number of jumps to reach the top of the mountain.

Constraints

• 1 ≤ T ≤ 50

• 2 ≤ N ≤ 10000

• 0 ≤ Ai ≤ 109 , where Ai represents i-th height of the stair. • Ai < Aj for every index i < j

Sample Input (standard input):

2

5

1 2 3 4 5

5

1 2 3 4 6

Sample Output (standard output):

Case #1: 1 4

Case #2: 2 3

Explanation In case 1, Jojo will jump 1 unit high because the biggest difference between adjacent stairs is 1. Then Jojo will jump 4 times to reach the top.

In case 2, Jojo will jump 2 units high because the biggest difference between adjacent stairs is 2 on the 4-th and 5-th stairs. Then Jojo made 3 jumps to reach the top with the following simulation.

• In jump 1, Jojo will jump from stair 1 to 3, because the height difference between the 1st and 3rd stairs is still less equal than 2.

• On jump 2, Jojo will jump from stair 3 to 4, with a height difference of 1. Jojo cannot jump directly to stair 5 because the difference in height exceeds the height of Jojo’s jump.

• On jump 3, Jojo will jump from stair 4 to 5, with a height difference of 2

NOTE: USE C LANGUAGE, DONT USE FUNCTION(RESULT,RETURN),VOID,RECURSIVE, USE BASIC CODE AND CODE IT UNDER int main (){, constraint must be the same

Solutions

Expert Solution

For this question, C code given below :-

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int cases,stairs,d=0,i,count=0,count1=0;
    int result[10],result1[10],v[10];

    printf("Enter number of test case :"); // get number of test cases
    scanf("%d",&cases);
  
    while(cases != 0 ) // while number of test cases not equal to zero
    {
        printf("case :%d\n",count1+1);
        printf("Enter numbers of stairs :"); // get number of stairs
        scanf ("%d",&stairs);
      
     
    for(i=0; i < stairs; i++)
        {
            fscanf(stdin, "%d", &v[i]); // get height of the each stair
        }
     
       // now , find difference between adjacent stairs
     d= v[1]-v[0];
   
     for (i=1;i<stairs-1;i++)
     {
       
         if (d <= v[i+1] - v[i])
         {
             d = v[i+1] - v[i];
         }
     }
    i=0;
  
    if (d==1) // if difference is 1 then total jump is number of stairs minus 1.
    {
        result[count1]=d;  
        result1[count1]=stairs-1;
        count1++;
        cases--;
    }
    else
    {
    while (i < stairs) //if difference is more then one
     {
       
         if (v[i+d] - v[i] <= d)
         {
            count++;
            i=i+d;
         }
         else if (v[i+d] - v[i] > d)
         {
             if (v[i+d-1] - v[i] <= d)
           {
                 count++;
                 i=i+d-1;
           }
         }
     }
      
    result[count1]=d;  
    result1[count1]=count;
    count1++;
    count=0;
    d=0;
    cases--;
     }
    }
     for (i=0;i<count1;i++)
     {
         printf("\nCase #%d : %d %d",i+1,result[i],result1[i]); // print each case
     }
  
    return 0;
}

Screenshot of code and output :-


Related Solutions

Theresa Nunn is planning a​ 30-day vacation on Pulau​ Penang, Malaysia, one year from now. The...
Theresa Nunn is planning a​ 30-day vacation on Pulau​ Penang, Malaysia, one year from now. The present charge for a luxury suite plus meals in Malaysian ringgit​ (RM) is RM1,043​/day. The Malaysian ringgit presently trades at RM3.1350​/$. She determines that the dollar cost today for a​ 30-day stay would be $9,980.86. The hotel informs her that any increase in its room charges will be limited to any increase in the Malaysian cost of living. Malaysian inflation is expected to be...
Theresa Nunn is planning a​ 30-day vacation on Pulau​ Penang, Malaysia, one year from now. The...
Theresa Nunn is planning a​ 30-day vacation on Pulau​ Penang, Malaysia, one year from now. The present charge for a luxury suite plus meals in Malaysian ringgit​ (RM) is RM1,043​/day. The Malaysian ringgit presently trades at RM3.1350​/$. She determines that the dollar cost today for a​ 30-day stay would be $9,980.86. The hotel informs her that any increase in its room charges will be limited to any increase in the Malaysian cost of living. Malaysian inflation is expected to be...
Each day, Luke counts how many licks it takes to get the center of a candy...
Each day, Luke counts how many licks it takes to get the center of a candy bar. After 30 days, Luke knows the average number of licks it takes to get to the center of a candy bar is 428. Assume that the distribution of licks is normal distributed with a standard deviation of 20. a) On day 31, it takes Luke 388 licks to get to the center of the candy bar. What is the probability that it only...
Employees earn vacation pay at the rate of one day per month. During the month of...
Employees earn vacation pay at the rate of one day per month. During the month of June, 10 employees qualify for one vacation day each. Their average daily wage is $150 per day. Which of the following is the necessary adjusting journal entry to record the June vacation benefits?
How much energy could we get from flattening the Rocky Mountains? Guesstimate how much potential energy...
How much energy could we get from flattening the Rocky Mountains? Guesstimate how much potential energy is stored in a mountain range.
One day, investor A takes a long position in 10 contracts of a certain futures, and...
One day, investor A takes a long position in 10 contracts of a certain futures, and investor B takes a short position in 10 contracts of the same futures. If this is the only transaction for the futures on the day, the trading volume for the futures is 20. Group of answer choices True/ False
Pulau Penang Island Resort. Theresa Nunn is planning a​ 30-day vacation on Pulau​ Penang, Malaysia, one...
Pulau Penang Island Resort. Theresa Nunn is planning a​ 30-day vacation on Pulau​ Penang, Malaysia, one year from now. The present charge for a luxury suite plus meals in Malaysian ringgit​ (RM) is RM1,041​/day. The Malaysian ringgit presently trades at RM3.1350​/$. She determines that the dollar cost today for a​ 30-day stay would be $9,961.72. The hotel informs her that any increase in its room charges will be limited to any increase in the Malaysian cost of living. Malaysian inflation...
Malaysian Island Resort. Theresa Nunn is planning a​ 30-day vacation on Pulau​ Penang, Malaysia, one year...
Malaysian Island Resort. Theresa Nunn is planning a​ 30-day vacation on Pulau​ Penang, Malaysia, one year from now. The present charge for a luxury suite plus meals in Malaysian ringgit​ (RM) is 1,044​/day. The Malaysian ringgit presently trades at RM3.1350​/$. She determines that the dollar cost today for a​ 30-day stay would be $9,990.43. The hotel informs her that any increase in its room charges will be limited to any increase in the Malaysian cost of living. Malaysian inflation is...
Suppose you know that the amount of time it takes your friend Susan to get from...
Suppose you know that the amount of time it takes your friend Susan to get from her residence to class averages  50 minutes, with a standard deviation of 55 minutes. What proportion of Susan's trips to class would take more than  50 minutes? . Enter your answers accurate to two decimal places. What proportion of her class would take more than  50 minutes? What proportion of Susan's trips to class would take less than 40 minutes? What proportion of Susan's trips to class...
Kroger runs Buy One Get One (BOGO) promotions every year to promote charcoal for Memorial Day....
Kroger runs Buy One Get One (BOGO) promotions every year to promote charcoal for Memorial Day. Their data indicates that 76 customers per day per store purchase this deal every spring and the standard deviation historically is 12 customers. Assume that the population is normally distributed. What is the probability that for a random sample of 15 days, at least 70 customers per day will buy charcoal with the BOGO? 1) 0.6915 2) 0.9772 3) 0.0262 4)0.9738
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT