日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

集训01-03 (c++实现)

發布時間:2023/12/13 c/c++ 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 集训01-03 (c++实现) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#include<bits/stdc++.h>與using namespace std;在第一第二行加上就行,無需了解
cin cout endl為c++的輸入,輸出與換行符
Istringsteam 是string流,用來string轉換為int
五個函數(需要稍微了解c++迭代器,lambda(類似函數))
count_if()
reverse()
sort()
accumulate()
replace()
三個容器
vector數組 其函數push_back()最為常用
string 字符串
map關聯容器,映射

集訓01
7-1 統計比指定元素大的個數-hebust

#include<bits/stdc++.h> using namespace std; int main() {int item,a;cin>>a;vector<int>_a;while(cin>>item){_a.push.back(item);}cout<<count_if(_a.begin(),_a.end(),[a](int x){return x>a;}); //count_if函數 返回滿足第三個參數的個數 //[a](int x){return x>a;} 是一個lambda }

7-2 倒順字母字符串-hebust

#include<bits/stdc++.h> using namespace std; int main() {string a = "abcdefghijklmnopqrstuvwxyz";vector<char>_a;int b,flag=0;cin >> b;for (auto i = a.begin(); i != a.begin() + b; ++i)//auto 編譯器會自動生成對應的類型 //i的類型是迭代器{if (b == 1)cout << *i;else{cout << *i << ' ';}_a.push_back(*i);}reverse(_a.begin(), _a.end());//使容器倒置for (auto i = _a.begin()+1; i != _a.end() ; ++i){if (flag == 1)cout << ' ';cout << *i;flag = 1;}}

集訓02
7-1 重復數據問題-hebust

#include<bits/stdc++.h> using namespace std; int main() {int item,a,b;vector<int>_a;while (cin >> item){_a.push_back(item);}sort(_a.begin(), _a.end());//排序算法auto iter = unique(_a.begin(), _a.end());if (iter != _a.end())cout << "yes";elsecout << "no"; }

7-2 找出最長的單詞-hebust

#include<bits/stdc++.h> using namespace std; int main() {string a;vector<string>_a;while (cin >> a){_a.push_back(a);}sort(_a.begin(), _a.end());cout << _a[_a.size() - 1]; }

7-3 較為復雜情況下的求和-hebust

#include<bits/stdc++.h> #include<cctype> using namespace std; int main() {vector<string>_a;string a;int s=0;while(cin>>a){_a.push_back(a);}for(auto i:_a){if(isalpha(i[0])){continue;}else{istringstream a1(i);//string流 用來string轉換為intint j;a1>>j;s+=j;}}cout<<s;}

集訓03
7-2 編程題:選修課成績統計問題

#include<bits/stdc++.h> using namespace std; map<string, int>B{ {"A",5},{"B",4}, {"C",3}, {"D",2},{"E",1} }; int main() {int flag=0,s;vector<string>A;string item,c;string n, m;while (cin>>item){A.push_back(item);A.push_back(" ");}c = accumulate(A.begin(), A.end(), string(""));//求和函數replace(c.begin(), c.end(), ',', ' ');//替換函數istringstream is(c);while(is>>item>>n>>m){s=B[n] + B[m];if (flag == 1)cout << ',';cout << item << ' ' << s;flag = 1;} }

7-3 找到共同的選修課-hebust

#include<bits/stdc++.h> using namespace std; int main() {string a;string b;int max = -1, c;cin >> c;map<string, int>_a;for (int i = 0; i < c; ++i){while (getchar() != ':'){}do{if(cin.good()) //這里有個坑,平臺第三組數據最后一行沒有換行符,需要檢查是否到文件尾{cin>>a;}else{break;}++_a[a];} while (getchar() != '\n');}for (auto i : _a){if (max < i.second){max = i.second;b = i.first;}}if (max!=c){cout << "none";}else{cout << b;} }

總結

以上是生活随笔為你收集整理的集训01-03 (c++实现)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。