WEEK:6 PROGRAMMING ASSIGNMENT 1 SOLUTION (Jan 2020-Apr2020) An Introduction to Programming through C++ NPTEL 2020

  

               An Introduction To Programming Through C++


IIT Bombay.



Week 6- Programming Assignment 1:


Suppose I have a red block (R) of height 1, a blue block (B) of height 2, and a green block (G) of height 3.  I can stack the blocks one above the other to build a tower.  For example, I can stack in the order RBG and I would get a tower of height 6.  I wish to compute how many different towers of a given height n can be built if there are ample blocks of each colour.

Let T(n) denote the number of towers of height n.

Write a program that prints T(n) given n.

Sample Test Cases
Input Output
Test Case 1
1
1
Test Case 2
2
2
Test Case 3
3
4
Test Case 4
4
7
Test Case 5
5
13
Test Case 6
-10
0
Test Case 7
29
29249425

WRITE/COPY THESE CODE DIRECTLY IN NPTEL TERMINAL AND COMPILE AND RUN THEN SUBMIT.

PROGRAM:



#include <iostream>

using namespace std;

long int Tower(int w, int x, int w_x, int y, int w_y, int z, int w_z){
    if(w < 0){
  return 0;
    } else if(w==0){
  return 1;
    }
   
    return Tower(w-w_x,x-1,w_x,y,w_y,z,w_z) + Tower(w-w_y,x,w_x,y-1,w_y,z,w_z) + Tower(w-w_z,x,w_x,y,w_y,z-1,w_z);
}

int main(){

    int G=3, B=2, R=1;
    int h;
    
    cin>>h;

    int w_g = h / G; // maximum G can be used
    int w_b = h / B; // maximum B can be used
    int w_r = h / R; // maximum B can be used
    
    int counts = 0; 
    if(h > 0){
        counts = Tower(h,w_g,G,w_b,B,w_r,R);
    }

   
    cout<< counts<<"\n";

    return 0;
}



WEEK-6 :PROGRAMMING ASSIGNMENT 2 
CLICK ON BELOW LINK.
<<CLICK HERE FOR  WEEK 6:PROGRAMMING -01>>

<<CLICK HERE FOR  WEEK 6: PROGRAMMING-02>> FOLLOW OUR WEBSITE FROM THE BUTTON PROVIDED TO THE BOTTOM OF PAGE TO GET SOON ANSWERS OF PROGRAMS.
Disclaimer: Here you can find all nptel assignment solutions related to CS stream.These may help you for your assignment.The answers are only for verification.You cannot copy these directly from the post(code of conduct of NPTEL).These is not 100%  correct solutions.

No comments:

Post a Comment

If you have any problems related to solutions or any concept please let me know.

Copyright (c) 2020 Custom Programs All Right Reserved

Pages