fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int correct_digit, correct_place;
  5. int possible_codes[10] = {0}; // Initialize all possible codes to 0
  6.  
  7. // Clue from 983: one digit is correct and in its right place
  8. possible_codes[9] = 2; // 9 is in the right place
  9. possible_codes[8] = 1; // 8 is incorrect
  10. possible_codes[3] = 1; // 3 is incorrect
  11.  
  12. // Clue from 914: one digit is correct but in the wrong place
  13. possible_codes[1] = 1; // 1 is incorrect
  14.  
  15. // Since 4 is not marked as incorrect from the first clue and not in the correct place from second clue,
  16. // it must be correct but not at position 3 (0 based index)
  17. possible_codes[4] = 2; // 4 is in the wrong place, but correct
  18.  
  19. // Clue from 309: two digits are correct but in the wrong places
  20. possible_codes[0] = 2; // 0 is correct but in the wrong place
  21.  
  22. // Clue from 728: all digits are incorrect
  23. possible_codes[7] = 1; // 7 is incorrect
  24. possible_codes[2] = 1; // 2 is incorrect
  25.  
  26. // Clue from 780: one digit is correct but in the wrong place
  27. // We have already marked 0 as correct from previous clue.
  28.  
  29. // Let's determine the correct code now
  30. int code[3] = {-1, -1, -1}; // Initialize code with -1
  31.  
  32. // If a digit is marked with 2, it's in the correct code but we don't know the position yet
  33. for (int i = 0; i < 10; i++) {
  34. if (possible_codes[i] == 2) {
  35. // Find the correct position for digit i
  36. for (int pos = 0; pos < 3; pos++) {
  37. // 4 cannot be in the third position (2 in 0 based index), 0 cannot be in the first position (0 in 0 based index)
  38. if ((i == 4 && pos != 2) || (i == 0 && pos != 0) || (i == 9 && pos == 0)) {
  39. code[pos] = i;
  40. break;
  41. }
  42. }
  43. }
  44. }
  45.  
  46. // Print the correct code
  47. printf("The code is: ");
  48. for (int i = 0; i < 3; i++) {
  49. printf("%d", code[i]);
  50. }
  51. printf("\n");
  52.  
  53. return 0;
  54. }
Success #stdin #stdout 0s 5304KB
stdin
45
stdout
The code is: 90-1