#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;}
}