众数问题: 给定含有n个元素的多重集合S,每个元素在S中出现的次数称为该元素的重数。多重集S中重数最大的元素称为众数。 求多重集S中的众数和重数。 #include <fstream> #include <hash_map> #include <utility> using namespace std; using namespace stdext; typedef pair <double, int> Num_Pair; int main() { ifstream fin("input.txt"); ofstream fout("output.txt"); hash_map<double, int> hm; int n; double num; double val; int fre = 0; hash_map<double, int>::iterator it; fin >> n; while (n--) { fin >> num; it = hm.find(num); if (hm.end() == it) { hm.insert(Num_Pair(num, 1)); if (!fre) { val = num; fre = 1; } } else { it->second++; if (it->second > fre) { fre = it->second; val = it->first; } } } fout << val << endl << fre <<endl; return 0; }