第1题
我的代码,
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
int a[30010];//学生初始成绩,从a[1]开始
int n, m;
int main()
{
while(cin >> n >> m)
{
for(int i = 1; i <= n; i++)
cin >> a[i];
int t = m;
char op;
while(m--)
{
cin >> op;
if(op == 'Q')
{
int t1, t2;
cin >> t1 >> t2;
int val1 = min(t1, t2);
int val2 = max(t1, t2);
t1 = val1;
t2 = val2;
priority_queue<int> h;
for(int i = t1; i <= t2; i++)
h.push(a[i]);
cout << h.top() << endl;
}
else
{
int t1, t2;
cin >> t1 >> t2;
a[t1] = t2;
}
}
}
return 0;
}
第2题
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(pair<string, int> a, pair<string, int> b)
{
return a.second > b.second;
}
int main() {
vector<pair<string, int>> errors;
string line, file;
while(getline(cin, line))
{
int pos = line.rfind('\\') + 1;
file = line.substr(pos);
bool is_have = false;
for(int i = 0; i < errors.size(); i++)
if(errors[i].first == file)
{
errors[i].second++;
is_have = true;
break;
}
if(!is_have) errors.push_back({file, 1});
}
stable_sort(errors.begin(), errors.end(), cmp);
for(int i = 0; i < 8 && i < errors.size(); i++)
{
string check = errors[i].first;
int t = check.find(' ');
if(t > 16) check.erase(0, t - 16);
cout << check << ' ' << errors[i].second << endl;
}
return 0;
}
第3题
我写的代码,
//检查空格的数目,来确定手牌的数目
//输入已经保证合法性,不用检查输入是否是合法的牌。
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string str;
//cin >> str //错误,不能这样使用,cin碰到空格或换行,就终止读入!!!
getline(cin, str);
if(str.find("joker JOKER") != -1)
{
cout << "joker JOKER" << endl;
return 0;
}
int index = str.find('-');
string str1 = str.substr(0, index);
string str2 = str.substr(index+1);
int num1 = count(str1.begin(), str1.end(), ' ');
int num2 = count(str2.begin(), str2.end(), ' ');
string world1 = str1.substr(0, str1.find(' '));
string world2 = str2.substr(0, str2.find(' '));
string s = "345678910JQKA2jokerJOKER";
if(num1 == num2)
{
if(s.find(world1) > s.find(world2))
cout << str1 << endl;
else
cout << str2 << endl;
}
else if(num1 == 3)
cout << str1 << endl;
else if(num2 == 3)
cout << str2 << endl;
else
cout << "ERROR" << endl;
return 0;
}