fork(1) download
  1. #include <iostream>
  2. #include <set>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <cassert>
  6.  
  7. class ThirteenCoins
  8. {
  9. public:
  10. ThirteenCoins(int coinWeight, int fakeCoinWeight, int fakeCoinId)
  11. : coinWeight(coinWeight), coinWeightDiff(fakeCoinWeight-coinWeight),
  12. fakeCoinId(fakeCoinId) {}
  13. char compare(const std::set<int>& lhs, const std::set<int>& rhs)const
  14. {
  15. std::vector<int> commonCoins;
  16. std::set_intersection(begin(lhs), end(lhs), begin(rhs), end(rhs),
  17. std::back_inserter(commonCoins));
  18. if (!commonCoins.empty()) throw "Some coins appear on both sides";
  19.  
  20. int lhsWeight = lhs.size() * coinWeight;
  21. int rhsWeight = rhs.size() * coinWeight;
  22. if (lhs.find(fakeCoinId) != end(lhs)) lhsWeight += coinWeightDiff;
  23. if (rhs.find(fakeCoinId) != end(rhs)) rhsWeight += coinWeightDiff;
  24. if (lhsWeight == rhsWeight) return 'B';
  25. return lhsWeight < rhsWeight ? 'R' : 'L';
  26. }
  27. private:
  28. int coinWeight;
  29. int coinWeightDiff;
  30. int fakeCoinId;
  31. };
  32.  
  33. int fakeCoinId(const ThirteenCoins& p)
  34. {
  35. static std::vector<std::string> codeWords = {
  36. "RRR", "LLL", "BBR", "BBL", "BRL", "BLR", "BRB", "BLB", "BRR",
  37. "BLL", "RLL", "LRR", "RLB", "LRB", "LRL", "RLR", "RBL", "LBR",
  38. "LBB", "RBB", "RBR", "LBL", "LLR", "RRL", "LLB", "RRB", "BBB"
  39. };
  40. std::string comp;
  41. comp += p.compare({ 7, 9,11,12}, { 5, 6, 8,10});
  42. comp += p.compare({ 5, 6,11,12}, { 2, 3, 4, 7});
  43. comp += p.compare({ 2, 5, 7, 8}, { 1, 4,10,11});
  44. return std::distance(begin(codeWords),
  45. std::find(begin(codeWords), end(codeWords), comp)) / 2;
  46. }
  47.  
  48. int main()
  49. {
  50. for (int i = 1; i <= 13; ++i)
  51. {
  52. assert(fakeCoinId(ThirteenCoins(10, 11, i)) == i);
  53. assert(fakeCoinId(ThirteenCoins(10, 9, i)) == i);
  54. }
  55. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
Standard output is empty