The results:
Input the vector number:9
Put the 4 and 4on the balance.
Put the 4 and 4on the balance.
Put the 4 and 2on the balance.
The weight of is fake coin.is2
The codes:
#include <iostream>
#include <vector>
using namespace std;
void fill_vector(vector<double> &quantity, int n);
double find_fake_coin(vector<double> quanity, int n);
int main()
{
cout << "Input the vector number:";
int number;
cin >> number;
vector<double> quantity;
fill_vector(quantity, number);
//cout << quantity[4];
double fake_coin_weight;
fake_coin_weight = find_fake_coin(quantity, number-1);
cout << "The weight of " << "is fake coin.is" << fake_coin_weight;
return 0;
}
void fill_vector(vector<double> &quantity, int n)
{
for(int i=0; i<n; ++i){
if(i==5){
quantity.push_back(2);
}else{
quantity.push_back(4);
}
}
}
double find_fake_coin(vector<double> quanity, int n)
{
//if(n==1){
// return min(quanity[1],quanity[0]);
// }
cout << "Put the " <<quanity[n] << " and " << quanity[n-1] << "on the balance."<< endl ;
if(quanity[n]!= quanity[n-1]) return min(quanity[n], quanity[n-1]);
return min(quanity[n],find_fake_coin(quanity, --n));
}