(1)输入两组字符串,要求在第一组字符串中查询第二组中的字符串,若存在,则将字符串中所以字符用*号代替。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1, str2;
while (cin >> str1 >> str2)
{
int t = str1.find(str2);
if (t >= 0)
{
for (int i = 0; i < str2.length(); i++)
{
str1[t + i] = '*';
}
}
cout << str1 << endl;
}
return 0;
}
(2) 输入描述:
输入包括多组测试数据。
每组输入第一行是两个正整数N和M(0 < N <= 30000,0 < M < 5000)N表示初始数据个数,M表示一共M组查询更新操作。
第二行包含N个整数
接下来又M行,每一行有一个字符C(只取‘Q’或‘U’),和两个正整数A,B,当C为’Q’的时候, 表示这是一条询问操作,他询问ID从A到B(包括A,B)的平均值
当C为‘U’的时候,表示这是一条更新操作,要求把ID为A的数值加上B。
输入为:
6 8
1 2 3 4 5 6
Q 1 6
U 2 6
U 4 3
Q 2 4
Q 1 2
U 1 3
U 2 1
Q 1 3
输出为:
3
6
4
5
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int M, N;
while (cin >> M >> N)
{
vector<int> num, result;
int begin = 0;
int end = 0;
char c;
for (int i = 0; i < M; i++)
{
int tmp = 0;
cin >> tmp;
num.push_back(tmp);
}
for (int j = 0; j < N; j++)
{
cin >> c;
cin >> begin;
cin >> end;
if (c == 'Q')
{
int sum = 0;
for (int j = begin - 1; j < end; j++)
{
sum += num[j];
}
int average = floor(sum / (end - begin + 1));
result.push_back(average);
}
else if (c == 'U')
{
num[begin - 1] += end;
}
}
for (int k = 0; k < result.size(); k++)
{
cout << result[k] << endl;
}
}
return 0;
}
(3)统计训练能力值
之前每有一天比当前天失误次数多,则当前天能力值-1
之前每有一天比当前天失误次数少,则当前天能力值+1
之前每有一天比当前天失误次数多,则当前天能力值不变
初始的能力值为0。
输入描述
输入一字数字T,表示T组测试数据
对于每组测试数据,第一行输入一个数n,表示小立训练天数,第二行输入n个数字,a1,a2......an表示小立的每天失误次数
1<=n ,ai <= 100000
输出描述
对于每组训练测试数据,输出两个数表示小立能力值最高为多少,以及训练结束后小立最终能力值多少。
输入为:
2
3
1 3 2
3
2 1 3
输出为:
1 1
1 1
/*第三题*/
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int round = 0;
int day;
vector<int> error, ability, result;
while (cin >> round)
{
int roundtmp = round;
while (roundtmp--)
{
cin >> day;
int tmp;
error.clear();
ability.clear();
for (int i = 0; i < day; i++)
{
cin >> tmp;
error.push_back(tmp);
}
for (int j = 0; j < day; j++) //求每天的能力值
{
if (j == 0)
{
ability.push_back(0);
}
else
{
int tmpability = ability[j - 1];
for (int m = 0; m < j; m++)
{
if (error[m] < error[j])
{
tmpability++;
}
else if (error[m] > error[j])
{
tmpability--;
}
}
ability.push_back(tmpability);
}
}
result.push_back(*max_element(ability.begin(), ability.end()));
result.push_back(ability[day - 1]);
//cout << *max_element(ability.begin(), ability.end()) << " " << ability[day - 1] << endl;
}
for (int k = 0; k < round; k++)
{
cout << result[2*k] << " " << result[2*k+1] << endl;
}
}
return 0;
}