Skip to content

Instantly share code, notes, and snippets.

@nick-ivanov
Last active September 22, 2020 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nick-ivanov/0c93c9014357efa2f5fa8d67d9620ed5 to your computer and use it in GitHub Desktop.
Save nick-ivanov/0c93c9014357efa2f5fa8d67d9620ed5 to your computer and use it in GitHub Desktop.
Determining the weight of a non-fake coin in constant time
#include <iostream>
#include <vector>
using namespace std;
int weigh(int w1, int w2) {
cout << "WEIGHING EVENT" << endl;
return w1 - w2;
}
int main()
{
int n;
cout << "Number of coins (min = 3): ";
cin >> n;
vector<int> v(n);
for(int i = 0; i < n; i++) {
cout << "Coin #" << (i + 1) << " weight: ";
cin >> v[i];
}
int good_weight = v[0];
bool heavier = false;
if(weigh(v[0], v[1]) != 0) {
if(weigh(v[0], v[2]) != 0) {
good_weight = v[1];
}
}
cout << "The non-counterfeit weight is: " << good_weight << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment