forked from careercup/CtCI-6th-Edition-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added some solutions to questions from Ch.6 and Ch.7
- Loading branch information
1 parent
5012155
commit a8bc03b
Showing
6 changed files
with
355 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
Ch 6. Math and Logic Puzzles/7.The Apocalypse/The Apocalypse.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <string> | ||
#include <vector> | ||
#include <iostream> | ||
|
||
void std::vector<int> runOneFamily() | ||
{ | ||
Random *random = new Random(); | ||
int boys = 0; | ||
int girls = 0; | ||
while (girls == 0) | ||
{ | ||
if (random->nextBoolean()) | ||
{ | ||
girls += 1; | ||
} | ||
else | ||
{ | ||
boys += 1; | ||
} | ||
} | ||
std::vector<int> genders = {girls, boys}; | ||
return genders; | ||
} | ||
|
||
double runNFamilies(int n) | ||
{ | ||
int boys = 0; | ||
int girls = 0; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
std::vector<int> genders = runOneFamily(); | ||
girls += genders[0]; | ||
boys += genders[1]; | ||
} | ||
return girls / static_cast<double>(boys + girls); | ||
} | ||
|
||
static void main(std::vector<std::wstring> &args) | ||
{ | ||
double ratio = runNFamilies(10000000); | ||
std::wcout << ratio << std::endl; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int i,j,sq; | ||
int min; | ||
for(sq = 2; sq <= 10; sq++) | ||
{ | ||
min = (sq-1)*(sq-1); | ||
min = min + (min+1)%2; | ||
for(i = min; i < sq*sq; i+=2) | ||
{ | ||
for(j = 3; j <= sq; j+=2) | ||
{ | ||
if (i%j == 0) | ||
bad; | ||
} | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
Ch 6. Math and Logic Puzzles/Intro/SieveOfEratosthenes.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include <string> | ||
#include <vector> | ||
#include <iostream> | ||
#include <cmath> | ||
|
||
void crossOff(std::vector<bool> &flags, int prime) | ||
{ | ||
for (int i = prime * prime; i < flags.size(); i += prime) | ||
{ | ||
flags[i] = false; | ||
} | ||
} | ||
|
||
int getNextPrime(std::vector<bool> &flags, int prime) | ||
{ | ||
int next = prime + 1; | ||
while (next < flags.size() && !flags[next]) | ||
{ | ||
next++; | ||
} | ||
return next; | ||
} | ||
|
||
void init(std::vector<bool> &flags) | ||
{ | ||
flags[0] = false; | ||
flags[1] = false; | ||
for (int i = 2; i < flags.size(); i++) | ||
{ | ||
flags[i] = true; | ||
} | ||
} | ||
|
||
void std::vector<int> prune(std::vector<bool> &flags, int count) | ||
{ | ||
std::vector<int> primes(count); | ||
int index = 0; | ||
for (int i = 0; i < flags.size(); i++) | ||
{ | ||
if (flags[i]) | ||
{ | ||
primes[index] = i; | ||
index++; | ||
} | ||
} | ||
return primes; | ||
} | ||
|
||
void std::vector<bool> sieveOfEratosthenes(int max) | ||
{ | ||
std::vector<bool> flags(max + 1); | ||
|
||
init(flags); | ||
int prime = 2; | ||
|
||
while (prime <= sqrt(max)) | ||
{ | ||
|
||
crossOff(flags, prime); | ||
|
||
|
||
prime = getNextPrime(flags, prime); | ||
} | ||
|
||
return flags; | ||
} | ||
|
||
void main(std::vector<std::wstring> &args) | ||
{ | ||
std::vector<bool> primes = sieveOfEratosthenes(4); | ||
for (int i = 0; i < primes.size(); i++) | ||
{ | ||
if (primes[i]) | ||
{ | ||
std::wcout << i << std::endl; | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
class BlackJackCard : public Card | ||
{ | ||
public: | ||
BlackJackCard(int c, Suit *s) : Card(c, s) | ||
{ | ||
} | ||
|
||
virtual int value() | ||
{ | ||
if (isAce()) | ||
{ | ||
return 1; | ||
} | ||
else if (faceValue >= 11 && faceValue <= 13) | ||
{ | ||
return 10; | ||
} | ||
else | ||
{ | ||
return faceValue; | ||
} | ||
} | ||
|
||
virtual int minValue() | ||
{ | ||
if (isAce()) | ||
{ | ||
return 1; | ||
} | ||
else | ||
{ | ||
return value(); | ||
} | ||
} | ||
|
||
virtual int maxValue() | ||
{ | ||
if (isAce()) | ||
{ | ||
return 11; | ||
} | ||
else | ||
{ | ||
return value(); | ||
} | ||
} | ||
|
||
virtual bool isAce() | ||
{ | ||
return faceValue == 1; | ||
} | ||
|
||
virtual bool isFaceCard() | ||
{ | ||
return faceValue >= 11 && faceValue <= 13; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include <vector> | ||
#include <limits> | ||
|
||
class BlackJackHand : public Hand<BlackJackCard*> | ||
{ | ||
public: | ||
BlackJackHand() | ||
{ | ||
|
||
} | ||
|
||
virtual int score() | ||
{ | ||
std::vector<int> scores = possibleScores(); | ||
int maxUnder = std::numeric_limits<int>::min(); | ||
int minOver = std::numeric_limits<int>::max(); | ||
for (auto score : scores) | ||
{ | ||
if (score > 21 && score < minOver) | ||
{ | ||
minOver = score; | ||
} | ||
else if (score <= 21 && score > maxUnder) | ||
{ | ||
maxUnder = score; | ||
} | ||
} | ||
return maxUnder == std::numeric_limits<int>::min() ? minOver : maxUnder; | ||
} | ||
|
||
private: | ||
void std::vector<int> possibleScores() | ||
{ | ||
std::vector<int> scores; | ||
if (cards->size() == 0) | ||
{ | ||
return scores; | ||
} | ||
for (BlackJackCard *card : cards) | ||
{ | ||
addCardToScoreList(card, scores); | ||
} | ||
return scores; | ||
} | ||
|
||
void addCardToScoreList(BlackJackCard *card, std::vector<int> &scores) | ||
{ | ||
if (scores.empty()) | ||
{ | ||
scores.push_back(0); | ||
} | ||
int length = scores.size(); | ||
for (int i = 0; i < length; i++) | ||
{ | ||
int score = scores[i]; | ||
scores[i] = score + card->minValue(); | ||
if (card->minValue() != card->maxValue()) | ||
{ | ||
scores.push_back(score + card->maxValue()); | ||
} | ||
} | ||
} | ||
|
||
public: | ||
virtual bool busted() | ||
{ | ||
return score() > 21; | ||
} | ||
|
||
virtual bool is21() | ||
{ | ||
return score() == 21; | ||
} | ||
|
||
virtual bool isBlackJack() | ||
{ | ||
if (cards->size() != 2) | ||
{ | ||
return false; | ||
} | ||
BlackJackCard *first = cards->get(0); | ||
BlackJackCard *second = cards->get(1); | ||
return (first->isAce() && second->isFaceCard()) || (second->isAce() && first->isFaceCard()); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include <string> | ||
#include <vector> | ||
#include <iostream> | ||
|
||
class Card | ||
{ | ||
private: | ||
bool available = true; | ||
|
||
protected: | ||
int faceValue = 0; | ||
|
||
Suit *suit_Renamed; | ||
|
||
public: | ||
Card(int c, Suit *s) | ||
{ | ||
faceValue = c; | ||
suit_Renamed = s; | ||
} | ||
|
||
virtual int value() = 0; | ||
|
||
virtual Suit *suit() | ||
{ | ||
return suit_Renamed; | ||
} | ||
|
||
|
||
virtual bool isAvailable() | ||
{ | ||
return available; | ||
} | ||
|
||
virtual void markUnavailable() | ||
{ | ||
available = false; | ||
} | ||
|
||
virtual void markAvailable() | ||
{ | ||
available = true; | ||
} | ||
|
||
virtual void print() | ||
{ | ||
std::vector<std::wstring> faceValues = {L"A", L"2", L"3", L"4", L"5", L"6", L"7", L"8", L"9", L"10", L"J", L"Q", L"K"}; | ||
std::wcout << faceValues[faceValue - 1]; | ||
switch (suit_Renamed) | ||
{ | ||
case Club: | ||
std::wcout << std::wstring(L"c"); | ||
break; | ||
case Heart: | ||
std::wcout << std::wstring(L"h"); | ||
break; | ||
case Diamond: | ||
std::wcout << std::wstring(L"d"); | ||
break; | ||
case Spade: | ||
std::wcout << std::wstring(L"s"); | ||
break; | ||
} | ||
std::wcout << std::wstring(L" "); | ||
} | ||
}; |