A - Bear and Raspberry
题目描述很复杂,其实就是找到相邻两数差最大。注意如果不存在要输出0。
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <iostream>
using namespace std;
int a[101];
int main() {
int n, c;
while (cin >> n >> c) {
for (int i = 0; i < n; ++ i) {
cin >> a[i];
}
int res = 0;
for (int i = 1; i < n; ++ i) {
int temp = a[i - 1] - a[i];
res = max(temp, res);
}
if (res - c < 0) cout << 0 << endl;
else cout << res - c << endl;
}
}
B - Bear and Strings
如果枚举两端,bear只有可能出现在首部或尾部,累加即可
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
string str;
int main() {
while (cin >> str) {
int res = 0;
for (int i = 0; i < str.length(); ++ i) {
for (int j = i + 1; j < str.length(); ++ j) {
if (j - i + 1 >= 4) {
if ((str[i] == 'b' && str[i + 1] == 'e' && str[i + 2] == 'a' && str[i + 3] == 'r')
|| (str[j - 3] == 'b' && str[j - 2] == 'e' && str[j - 1] == 'a' && str[j] == 'r')) {
res += str.length() - j;
//printf("%d %d\n", i, j); cout << str.substr(0, 3) << endl;
break;
}
}
}
}
cout << res << endl;
}
}
C - Bear and Prime Numbers
Eratosthenes素数筛法的变形,有几组边界数据,要注意处理。
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int vis[10000010];
int prime[10000010], res[10000010];
int main() {
int n;
while (cin >> n) {
int Max = 0;
for (int i = 0; i < n; ++ i) {
int x;
scanf("%d", &x);
vis[x] ++;
}
prime[0] = prime[1] = 1;
for (int i = 2; i <= 10000000; ++ i) {
if (!prime[i]) for (int j = i; j <= 10000000; j += i) {
prime[j] = 1;
if (vis[j]) {
res[i] += vis[j];
}
}
}
for (int i = 2; i <= 10000000; ++ i) {
res[i] += res[i - 1];
}
int m;
cin >> m;
while (m --) {
int l, r;
scanf("%d%d", &l, &r);
l = min(10000000, l); r = min(10000000, r);
int num = res[r] - res[l - 1];
cout << num << endl;
}
}
}