Last active
September 22, 2020 11:58
-
-
Save nick-ivanov/0c93c9014357efa2f5fa8d67d9620ed5 to your computer and use it in GitHub Desktop.
Determining the weight of a non-fake coin in constant time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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