//Дан набор монет, и ещё одна монета (эталон). Есть ли в наборе монета, равная
//по весу эталону?
//Я так понимаю, может и неправильно ;), что в наборе все монеты одинаковые по весу, и если в нем
//присутствует эталон, то только один.
#include <iostream> // cin, cout
#include <vector> // vector<>
#include <algorithm> // copy(), find()
#include <iterator> // istream_iterator<>, distance()
#include <conio.h> // getch()
using namespace std;
typedef vector<int> IntV;
int main ()
{
IntV c;
int metalon;
cout << "Enter weight of etalon\n"; cin >> metalon;
cout << "Enter weight of coin or -1 to end\n";
// тип unsigned взят специально,
// когда пользователь введет отрицательное число
// или любой символ, ввод прекратится
copy(istream_iterator<unsigned>(cin), istream_iterator<unsigned>(),
back_inserter(c));
// Поиск этанола в массиве
IntV::iterator etalon2(find(c.begin(), c.end(), metalon));
// если найден
if (etalon2 != c.end())
{
// ищем еще один
if (find(etalon2+1, c.end(), metalon) == c.end())
// найден только один
cout << "Yes, there is the second etalon in the array at position " << distance(c.begin(), etalon2);
else
// найдено два
cout << "There are more than two etalons in the array";
}
else
// не найдено ни одного
cout << "There is no more etalons in the array";
cout << endl;
getch();
return 0;
}
Если Вы уже зарегистрированы на Портале - войдите в систему, если Вы еще не регистрировались - пройдите простую процедуру регистрации.