Beispiel #1
1
int main() {
	const int SIZE = 20;
	int arr[SIZE] =
			{ 2, 1, 3, 3, 6, 6, 5, 5, 5, 8, 7, 4, 1, 1, 1, 1, 9, 6, 5, 4 };
	cout << "Array is:" << endl;
	for (int i =0; i<SIZE;i++)
	{
		cout << arr[i] << " ";
	}
	Ims greaterSub;
	greaterSub.insert(arr, arr + SIZE);
	Ims::iterator it;
	int sum = 0;
	for (it = greaterSub.begin(); it != greaterSub.end(); it++) {
		sum += *it;
	}
	int avg = sum / greaterSub.size();
	it = greaterSub.begin();
	unsigned int max = 0;
	int numb;
	while (it != greaterSub.end()) {
		if (*it > avg) {
			if (max < greaterSub.count(*it)) {
				max = greaterSub.count(*it);
				numb = *it;
			}
		}
		it = greaterSub.upper_bound(*it);
	}
	cout << "\nAverrage is: " << avg << endl;
	cout << "The longest subsequence of greater values in array is:\n";
	cout << numb << " -> " << max << " times." << endl;
	return 0;
}
Beispiel #2
0
int main() {

	cout << "Enter number of times: ";
	unsigned int n;
	cin >> n;

	const int SIZE = 20;
	int a[SIZE] = { 4, 2, 2, 5, 2, 3, 2, 3, 1, 5, 6,5,3,5,7,9,3,1,7,6};
	Ims intMultiset;
	intMultiset.insert(a, a + SIZE);
	ostream_iterator<int> output(cout, " ");
	copy(intMultiset.begin(), intMultiset.end(), output);
	cout << endl;
	Ims::iterator it;
	it = intMultiset.begin();
	while (it != intMultiset.end()) {
		if (intMultiset.count(*it) == n) {
			intMultiset.erase(intMultiset.lower_bound(*it),
					intMultiset.upper_bound(*it));
		} else {
			it = intMultiset.upper_bound(*it);
		}
	}
	copy(intMultiset.begin(), intMultiset.end(), output);
	return 0;
}