#include <iostream> using namespace::std; class coin { friend ostream & operator
ID: 651802 • Letter: #
Question
#include <iostream>
using namespace::std;
class coin {
friend ostream & operator << ( ostream &, const coin &);
friend bool operator == ( int n, const coin & right);
// will return true if n is equal to the count field of right
public:
coin(void);
coin(int ct, const char * d);
coin(const coin &);
~coin(void);
int getCount(void) const;
coin & setCount(int n);
coin & setDesc( const char *);
const coin & getLarger( const coin & ) const;
// will return as an alias the larger coin
// , i.e. larger of the invoking instance and the paramater
// use getCount() to determine the largest
const coin & operator = ( const coin &);
coin operator + ( const coin & ) const;
// return coin with counts added
// same desc as the invoking instance
bool operator == ( const coin &) const;
// return true if the counts are equal
bool operator == ( int v) const;
// return true if v is equal to the count field
private:
const char * getDesc(void) const;
int count;
char desc[25];
};
class bank {
friend ostream & operator<<(ostream &, const bank &);
public:
bank(void);
bank(int, const char * coinDesc, const char * ownerName);
bank(const bank &);
bank( const coin &, const char * ownerName);
~bank();
int getCount(void) const;
bank & changeOwner( const char *);
const bank & operator = (const bank &);
bool operator == ( const bank & ) const;
// will return true if the owners are equal as strings
// and coin fields data are equal as coins
bool operator != ( const bank &) const;
private:
coin data;
char * owner;
};
// Write the Class functions
Explanation / Answer
const int MAX_N = 100;
void printMoves(int P[][MAX_N], int A[], int N) {
int sum1 = 0, sum2 = 0;
int m = 0, n = N-1;
bool myTurn = true;
while (m <= n) {
int P1 = P[m+1][n]; // If take A[m], opponent can get...
int P2 = P[m][n-1]; // If take A[n]
cout << (myTurn ? "I" : "You") << " take coin no. ";
if (P1 <= P2) {
cout << m+1 << " (" << A[m] << ")";
m++;
} else {
cout << n+1 << " (" << A[n] << ")";
n--;
}
cout << (myTurn ? ", " : ". ");
myTurn = !myTurn;
}
cout << " The total amount of money (maximum) I get is " << P[0][N-1] << ". ";
}
int maxMoney(int A[], int N) {
int P[MAX_N][MAX_N] = {0};
int a, b, c;
for (int i = 0; i < N; i++) {
for (int m = 0, n = i; n < N; m++, n++) {
assert(m < N); assert(n < N);
a = ((m+2 <= N-1) ? P[m+2][n] : 0);
b = ((m+1 <= N-1 && n-1 >= 0) ? P[m+1][n-1] : 0);
c = ((n-2 >= 0) ? P[m][n-2] : 0);
P[m][n] = max(A[m] + min(a,b),
A[n] + min(b,c));
}
}
printMoves(P, A, N);
return P[0][N-1];
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.