使用vector存盤用戶從鍵盤輸入的n個整數,利用stl中的演算法,統計出現次數大于2的資料。
uj5u.com熱心網友回復:
vector<int> v = {1,2,3,4,5,6,7,2,3,0,10};cout << count_if(v.begin(), v.end(), [=](int count) {
return count > 2;
});
uj5u.com熱心網友回復:
看這時間,有點dlut的味道uj5u.com熱心網友回復:
int main() {
int n;
cin >> n; // 輸入一共有多少個資料
vector<int> vec; // 保存資料
int number;
// 從鍵盤輸入n個數
while (n--) {
cin >> number;
vec.push_back(number);
}
unordered_map<int, int> num_count;
// 統計資料出現的次數
for (auto &it : vec) {
if (num_count.count(it)) {
num_count[it]++;
}
else {
num_count.insert(pair<int, int>(it, 1));
}
}
// 輸出
for (auto &it : num_count) {
if (it.second > 2) {
cout << it.first << " ";
}
}
}例子:輸入7個數,其中22出現了3次。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/37717.html
標籤:C++ 語言
上一篇:16進制文本
下一篇:關于new后面的這種語法啥意思
