The Intelligent Smoker

Printer-friendly versionPDF version

A person smokes cigarettes and leaves the lower portion (around 1/5) of it unburnt. He then combines the 5 residues of those cigarettes to create a new one for smoking.

If the person had initially 93 cigarettes, how many cigarettes did he actually smoked?

He will smoke 116 cigarettes.

#include <utility>
#include <iostream>

typedef std::pair<int,double> Cigarettes;
using namespace std;

#define PIECES_REQD_TO_CREATE_A_NEW_CIGARETTE 5

Cigarettes cigarette(Cigarettes count);

int main()
{  
  int initialCount = 0;
  cout << "Enter the number of initial cigarettes: ";
  cin >> initialCount;
  Cigarettes count = make_pair(initialCount, 0.0);
  cout << "The person smoked " << cigarette(count).first << " number of cigarettes." << endl;
  return 0;
}

Cigarettes cigarette(Cigarettes count)
{
  if(count.first > 0)
  {
    Cigarettes tmp = make_pair(count.first/PIECES_REQD_TO_CREATE_A_NEW_CIGARETTE,
      count.second + ((count.first%PIECES_REQD_TO_CREATE_A_NEW_CIGARETTE)/
      (PIECES_REQD_TO_CREATE_A_NEW_CIGARETTE+0.0)));
    if(tmp.second >= 1.0)
    {
      tmp.first += 1;
      tmp.second -= 1;
    }
    tmp = cigarette(tmp);
    count.first += tmp.first;
    count.second = tmp.second;
  }
  return count;
}



AttachmentSize
cigarettes.cpp975 bytes
No votes yet