Advertisement
B1KMusic

[easy] number permutation challenge solution

Mar 5th, 2017
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.94 KB | None | 0 0
  1. /*
  2.  * Challenge from: https://www.youtube.com/watch?v=2bkfA2fHVwg
  3.  *
  4.  * "Write a program to display all valid department number permutations"
  5.  *
  6.  * Department numbers
  7.  *
  8.  * Fire
  9.  * Police
  10.  * Sanitation
  11.  *
  12.  * police# must be even
  13.  * each in range 1 - 7
  14.  * must be unique
  15.  * must add up to 12
  16.  */
  17.  
  18. #include <stdio.h>
  19.  
  20. enum { d_fir, d_pol, d_san };
  21.  
  22. int
  23. incr(int *nums)
  24. {
  25.     int isbrk = 0;
  26.  
  27.     for(int i = 2; i >= 0; nums[i] = 1, i--)
  28.         if(++nums[i] <= 7 && (isbrk = 1))
  29.             break;
  30.  
  31.     return isbrk;
  32. }
  33.  
  34. int
  35. check(int *nums)
  36. {
  37.     return nums[d_pol] % 2 == 0
  38.         && nums[0] + nums[1] + nums[2] == 12
  39.         && nums[0] != nums[1]
  40.         && nums[0] != nums[2];
  41. }
  42.  
  43. int
  44. main(void)
  45. {
  46.     int nums[3] = {1,1,0};
  47.  
  48.     while(incr(nums))
  49.         if(check(nums))
  50.             printf("Fire#%i Police#%i Sanitation #%i\n",
  51.                     nums[d_fir], nums[d_pol], nums[d_san]);
  52.  
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement