void iterate() { printf("size = %d\n", numbers.size()); NumbersIter iter; for (iter = numbers.begin(); iter != numbers.end(); ++iter) { printf(" %d\n", *iter); } }
int main() { int tests; typedef map<string, int> Numbers; string x; int n; if(scanf("%d\n", &tests)); for(int t = 0; t < tests; ++t) { cin >> n; Numbers nums; for(int i = 0; i < n; ++i) { cin >> x; sanitise(x); Numbers::iterator it = nums.find(x); if(it == nums.end()) { it = nums.insert(Numbers::value_type(x, 0)).first; } it->second++; } Numbers::iterator it = nums.begin(); bool found = false; if(t > 0) printf("\n"); while(it != nums.end()) { if(it->second > 1) { printf("%s %d\n", it->first.c_str(), it->second); found = true; } ++it; } if(!found) { printf("No duplicates.\n"); } } return 0; }
bool ParallelComputeAverages(Numbers const &numbers, Averages &averages) { if (numbers.empty()) { return false; } // sort numbers for mode and median Numbers sorted = numbers; Averages avgResult; int modeResult; double medianResult; // sort numbers for median / mode tbb::parallel_sort(sorted); auto medianFunc = [&medianResult, &sorted]() { medianResult = ComputeMedian(sorted); }; auto avgFunc = [&avgResult, &numbers]() { auto grainSize = numbers.size() / thread::hardware_concurrency(); Averages avg{0, 0, 0, 0, 0, 0}; avgResult = tbb::parallel_reduce( Range(numbers.begin(), numbers.end(), grainSize), avg, CalcIntermediateAverages, AddIntermediateAverages); }; auto modeFunc = [&modeResult, &sorted]() { modeResult = ComputeMode(sorted); }; // invoke parallel execution of calculations tbb::parallel_invoke(avgFunc, modeFunc, medianFunc); // set result double size = (double) numbers.size(); averages.arithmeticMean = avgResult.arithmeticMean / size; averages.geometricMean = exp(avgResult.geometricMean / size); averages.harmonicMean = size / avgResult.harmonicMean; averages.quadraticMean = sqrt(avgResult.quadraticMean / size); averages.mode = modeResult; averages.median = medianResult; return true; }
bool ComputeAverages(Numbers const &numbers, Averages &averages) { if (numbers.empty()) { return false; } Numbers sortedNumbers = numbers; sort(sortedNumbers.begin(), sortedNumbers.end()); // sorting required for mode and median averages.mode = ComputeMode(sortedNumbers); averages.median = ComputeMedian(sortedNumbers); ComputeMeans(numbers, averages); return true; }
void PrintNumbers(Numbers const &numbers) { cout << "Numbers: "; copy(numbers.begin(), numbers.end(), ostream_iterator<int>(cout, " ")); cout << endl; }