Last active
August 12, 2017 01:53
-
-
Save theeternalsw0rd/18617416519490421cfb5aabb65acef9 to your computer and use it in GitHub Desktop.
Think like a programmer. Episode 2. Real solution
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
// I like to start with the one that has the narrowest set first. That's just personal preference. | |
// police department must have an even number. no need to loop to 7 which is odd. | |
for(int policeDepNum = 2; policeDepNum <= 6; policeDepNum += 2) { | |
for(int fireDepNum = 1; fireDepNum <= 7; fireDepNum++) { | |
// if these are equal there is no solution this loop so continue on. | |
if(fireDepNum == policeDepNum) { | |
continue; | |
} | |
// sum must equal 12. no need to loop for last number. | |
sanitDepNum = 12 - fireDepNum - policeDepNum; | |
// check that sanitDepNum meets all conditions and continue if not. | |
if(sanitDepNum < 1 || sanitDepNum > 7 || sanitDepNum == fireDepNum || sanitDepNum == policeDepNum) { | |
continue; | |
} | |
cout << "Police Department Number: " << policeDepNum << "\nFire Department Number: " << fireDepNum << "\nSanitation Department Number: " << sanitDepNum << "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment