Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Challenge from: https://www.youtube.com/watch?v=2bkfA2fHVwg
- *
- * "Write a program to display all valid department number permutations"
- *
- * Department numbers
- *
- * Fire
- * Police
- * Sanitation
- *
- * police# must be even
- * each in range 1 - 7
- * must be unique
- * must add up to 12
- */
- #include <stdio.h>
- enum { d_fir, d_pol, d_san };
- int
- incr(int *nums)
- {
- int isbrk = 0;
- for(int i = 2; i >= 0; nums[i] = 1, i--)
- if(++nums[i] <= 7 && (isbrk = 1))
- break;
- return isbrk;
- }
- int
- check(int *nums)
- {
- return nums[d_pol] % 2 == 0
- && nums[0] + nums[1] + nums[2] == 12
- && nums[0] != nums[1]
- && nums[0] != nums[2];
- }
- int
- main(void)
- {
- int nums[3] = {1,1,0};
- while(incr(nums))
- if(check(nums))
- printf("Fire#%i Police#%i Sanitation #%i\n",
- nums[d_fir], nums[d_pol], nums[d_san]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement