fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cassert>
  4.  
  5. struct Balance {
  6. int maxUses;
  7. Balance(int maxUses) : maxUses(maxUses) {}
  8. int compare(int a, int b)
  9. {
  10. if (--maxUses < 0) throw "Sử dụng quá số lần cân";
  11. return a - b;
  12. }
  13. };
  14.  
  15. struct Stones {
  16. std::vector<int> stones;
  17. Stones(int fakeId, int fakeWeight) : stones(12, 0)
  18. { stones[fakeId] = fakeWeight; }
  19. int operator[](int i)const { return stones[i-1]; } //-1
  20. };
  21.  
  22. int findFakeId(const Stones& s, Balance b)
  23. {
  24. int comp1 = b.compare(s[5]+s[6]+s[7]+s[8], s[9]+s[10]+s[11]+s[12]);
  25. if (!comp1) {
  26. int comp2 = b.compare(s[1]+s[2], s[5]+s[6]);
  27. if (comp2) return b.compare(s[1], s[5]) ? 1 : 2;
  28. else return b.compare(s[3], s[5]) ? 3 : 4;
  29. } else if (comp1 < 0) {
  30. int comp2 = b.compare(s[5]+s[6]+s[9], s[7]+s[8]+s[10]);
  31. if (comp2 < 0) {
  32. int comp3 = b.compare(s[5], s[6]);
  33. return comp3 ? (comp3 > 0 ? 6 : 5) : 10;
  34. } else if (comp2 > 0) {
  35. int comp3 = b.compare(s[7], s[8]);
  36. return comp3 ? (comp3 > 0 ? 8 : 7) : 9;
  37. } else return b.compare(s[11], s[1]) ? 11 : 12;
  38. } else {
  39. int comp2 = b.compare(s[5]+s[6]+s[9], s[7]+s[8]+s[10]);
  40. if (comp2 < 0) {
  41. int comp3 = b.compare(s[7], s[8]);
  42. return comp3 ? (comp3 > 0 ? 7 : 8) : 9;
  43. } else if (comp2 > 0) {
  44. int comp3 = b.compare(s[5], s[6]);
  45. return comp3 ? (comp3 > 0 ? 5 : 6) : 10;
  46. } else return b.compare(s[11], s[1]) ? 11 : 12;
  47. }
  48. return -1;
  49. }
  50.  
  51. int main()
  52. {
  53. for (int w = -1; w <= 1; w += 2)
  54. {
  55. std::cout << (w < 0 ? "Nhẹ" : "Nặng") << " hơn:\n";
  56. for (int i = 0; i < 12; ++i)
  57. {
  58. assert(findFakeId(Stones(i,w), 3) == i+1);
  59. std::cout << i+1 << " chính xác\n";
  60. }
  61. std::cout << "\n";
  62. }
  63. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
Nhẹ hơn:
1 chính xác
2 chính xác
3 chính xác
4 chính xác
5 chính xác
6 chính xác
7 chính xác
8 chính xác
9 chính xác
10 chính xác
11 chính xác
12 chính xác

Nặng hơn:
1 chính xác
2 chính xác
3 chính xác
4 chính xác
5 chính xác
6 chính xác
7 chính xác
8 chính xác
9 chính xác
10 chính xác
11 chính xác
12 chính xác