花了五个小时把题目整体做了一遍,现在平台没有判题机,不能测评,若有问题欢迎在评论区提出。题目整体难度不难,主要考察知识点:简单模拟、结构体排序、简单矩阵处理、字符串、STL应用、简单数论(素数、公倍数等等)、进制转换、日期问题、板子图论(最短路、求连通块)、裸背包问题、递推。
复试上机平台:南邮复试上机平台
#include
using namespace std;
int n, a, b, x;
int main() {
while (cin >> n) {
int maxv = -1, minv = 101;
for (int i = 1; i <= n; i++) {
cin >> x;
maxv = max(maxv, x); minv = min(minv, x);
}
cout << maxv << ' ' << minv << endl;
}
return 0;
}
#include
#include
using namespace std;
long long n, x;
bool check(long long x) {
if (x < 2 || x > 10000) return false;
for (int i = 2; i <= x / i; i++) {
if (x % i == 0)
return false;
}
string str = to_string(x);
string s = str;
reverse(s.begin(), s.end());
return s == str;
}
int main() {
cin >> n;
while (n --) {
cin >> x;
cout << (check(x) ? "Yes\n" : "No\n");
}
return 0;
}
#include
#include
using namespace std;
int t, n, r;
string work(int x, int r) {
string res;
bool flag = x < 0;
x = abs(x);
while (x) {
int k = x % r;
res += k <= 9 ? k + '0' : k - 10 + 'A';
x /= r;
}
res += flag ? "-" : "";
reverse(res.begin(), res.end());
return res;
}
int main() {
cin >> t;
while (t --) {
cin >> n >> r;
cout << work(n, r) << endl;
}
return 0;
}
#include
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << __gcd(a, b) << " " << a * b / __gcd(a, b) << endl;
return 0;
}
#include
using namespace std;
const int N = 40;
int n, f[N];
int main() {
cin >> n;
f[0] = 0, f[1] = f[2] = 1;
for (int i = 3; i <= n; i++) {
f[i] = f[i - 1] + f[i - 2];
}
cout << f[n] << endl;
return 0;
}
#include
using namespace std;
string str;
int main() {
cin >> str;
for (auto &c : str) c = tolower(c);
string s = str;
reverse(str.begin(), str.end());
cout << (s == str ? "Yes\n" : "No\n");
return 0;
}
#include
#define int long long
using namespace std;
typedef pair<int, int> PII;
const int N = 3000, M = 6500 * 2, inf = 0x3f3f3f3f;
int n, m, s, t, a, b, c;
int h[N], e[M], ne[M], w[M], idx;
int dist[N];
bool st[N];
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
void dijkstra() { // 堆优化dijkstra
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.emplace(0, s); // dist - sno
memset(dist, 0x3f, sizeof dist);
dist[s] = 0;
while (!heap.empty()) {
auto t = heap.top();
heap.pop();
int ver = t.second, distance = t.first;
if (st[ver]) continue;
st[ver] = true;
for (int i = h[ver]; ~i; i = ne[i]) {
int j = e[i];
if (dist[j] > distance + w[i]) {
dist[j] = distance + w[i];
heap.emplace(dist[j], j);
}
}
}
}
signed main() {
cin >> n >> m >> s >> t;
memset(h, -1, sizeof h);
while (m -- ) {
cin >> a >> b >> c;
add(a, b, c), add(b, a, c);
}
dijkstra();
cout << dist[t] << endl;
return 0;
}
#include
using namespace std;
const int N = 25;
int a[N], n, h, cnt;
signed main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
cin >> h;
for (int i = 1; i <= n; i++) {
if (h + 30 >= a[i])
cnt ++;
}
cout << cnt << endl;
cout << (cnt == n ? "Yes\n" : "No\n");
return 0;
}
#include
using namespace std;
int k;
vector<int> v;
signed main() {
v.push_back(0);
for (int i = 1; i <= 200; i++) {
for (int j = 1; j <= i; j++)
v.push_back(i);
}
cin >> k;
int res = 0;
for (int i = 1; i <= k; i++) {
res += v[i];
}
cout << res << endl;
return 0;
}
#include
using namespace std;
bool isprime(int x) {
if (x < 2) return false;
for (int i = 2; i <= x / i; i++)
if (x % i == 0)
return false;
return true;
}
bool iscomprime(int x) {
if (x < 2) return false;
for (int i = 2; i <= x / i; i++)
if (x % i == 0)
return true;
return false;
}
signed main() {
int n, ra, rb;
cin >> n;
while (1) {
n ++;
if (isprime(n) && !ra)
ra = n;
if (iscomprime(n) && !rb)
rb = n;
if (ra && rb) break;
}
//cout << ra << ' ' << rb << endl;
cout << ra + rb << endl;
return 0;
}
#include
using namespace std;
int k;
double s;
signed main() {
cin >> k;
for (int i = 1; ; i++) {
s += 1.0 * i / k;
if (s > k) {
cout << i;
return 0;
}
}
return 0;
}
#include
using namespace std;
string s[5], mp[5];
bool same = true;
double avg;
int up, down, resa, resb;
signed main() {
for (int i = 1; i <= 4; i++) {
cin >> s[i];
mp[i] = 'A' + i - 1, avg += s[i].size();
}
avg /= 4.0;
for (int i = 2; i <= 4; i++) {
if (s[i].size() != s[i - 1].size())
same = false;
}
string shor = "0000000000000", lon = "";
for (int i = 1; i <= 4; i++) {
if (s[i].size() > avg) up ++;
else if (s[i].size() < avg) down ++;
if (s[i].size() < shor.size()) {
shor = s[i];
resa = i;
}
if (s[i].size() > lon.size()) {
lon = s[i];
resb = i;
}
}
if (up >= 3) cout << mp[resa] << '\n';
else if (down >= 3) cout << mp[resb] << '\n';
else if (same) cout << "B\n";
else cout << "C\n";
return 0;
}
#include
using namespace std;
int n, k;
signed main() {
cin >> n >> k;
int m = n / k, s = n;
while (m) {
s += m;
n = m + n % k;
m = n / k;
}
cout << s << endl;
return 0;
}
#include
using namespace std;
const int N = 250;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int n, k, r;
int g[N][N];
signed main() {
cin >> n >> k >> r;
while (k --) {
int x, y;
cin >> x >> y;
g[x][y] = 1;
for (int i = 0; i < 4; i++) {
int a = x + dx[i], b = y + dy[i];
if (a <= 0 || a > n || b <= 0 || b > n) continue;
g[a][b] = 1;
}
}
int res = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (g[i][j]) {
//cout << i << ' ' << j << endl;
res ++;
}
}
}
cout << res << endl;
return 0;
}
#include
using LL = long long;
using namespace std;
const int N = 1e3 + 7;
LL n, a[N];
signed main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a + 1, a + n + 1);
cout << a[n] + a[n - 1] + a[n - 2] << endl;
return 0;
}
#include
using LL = long long;
using namespace std;
const int N = 7e2 + 9;
int n, m, x;
int g[N][N];
bool st[N][N];
signed main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> x;
g[i][j] = x;
st[i][j] = 1;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (g[i][j] == 1 && st[i][j]) {
for (int k = 1; k <= n; k++)
g[k][j] = 0, st[k][j] = 0;
for (int k = 1; k <= m; k++)
g[i][k] = 0, st[i][k] = 0;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (j != 1) cout << ' ' << g[i][j];
else cout << g[i][j];
}
cout << endl;
}
return 0;
}
#include
using LL = long long;
using namespace std;
const int N = 7e2 + 9;
int n, m, x;
LL a[N];
unordered_map<LL, bool> st;
signed main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
st[a[i]] = true;
}
cin >> m;
while (m --) {
cin >> x;
cout << (st.count(x) ? "YES\n" : "NO\n");
}
return 0;
}
#include
using LL = long long;
using namespace std;
const int N = 1e4 + 9;
int n, m, x, l, a, b;
bool st[N];
signed main() {
cin >> l >> m;
while (m --) {
int a, b;
cin >> a >> b;
for (int i = a; i <= b; i++) st[i] = true;
}
cout << count(st, st + l + 1, 0) << endl;
return 0;
}
#include
using LL = long long;
using namespace std;
const int N = 7e2 + 9;
int n, m, x;
LL a[N], b[N];
unordered_map<LL, bool> st;
signed main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
cin >> m;
for (int i = 1; i <= m; i++) {
cin >> b[i];
}
int res = 0;
for (int i = 1; i <= n; i++) {
bool flag = false;
for (int j = 1; j <= m; j++) {
if (a[i] % b[j] == 0) {
flag = true;
break;
}
}
if (flag) res ++;
}
cout << res << endl;
return 0;
}
#include
using LL = long long;
using namespace std;
const int N = 7e2 + 9;
int n, m;
signed main() {
cin >> n >> m;
double res = 0;
for (int i = 1; i <= n; i++) {
double s = 0, x, minv = 100, maxv = -1;
for (int j = 1; j <= m; j++) {
cin >> x;
s += x;
maxv = max(maxv, x), minv = min(minv, x);
}
s = s - maxv - minv;
res = max(res, s);
}
printf("%.2f\n", res / (m - 2));
return 0;
}
#include
using namespace std;
int y, m, d, n;
int days[15] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30, 31};
bool isleap(int y) {
return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
}
void work(int year, int n) {
days[2] += isleap(year);
int m = 1, d = n;
while (d > days[m]) {
d -= days[m], m ++;
}
cout << m << ' ' << d << endl;
}
signed main() {
cin >> y >> n;
work(y, n);
return 0;
}
#include
using namespace std;
int n;
signed main() {
cin >> n;
while (n --) {
string s, q;
cin >> s >> q;
int res = 0;
for (int i = 0; i < s.size(); i++) {
string cur = s.substr(i, q.size());
if (cur == q)
res ++;
}
cout << res << endl;
}
return 0;
}
#include
using namespace std;
signed main() {
long long n, m, a;
cin >> n >> m >> a;
cout << ceil(1.0 * n / a) * ceil(1.0 * m / a) << endl;
return 0;
}
#include
using namespace std;
const int N = 1e4 + 7;
int n, a[N];
signed main() {
cin >> n;
for (int i = 0; i < n; i ++ ) {
cin >> a[i];
}
int cur = 1, res = 1;
for (int i = 1; i < n; i ++ ) {
if (a[i] > a[i - 1])
cur ++;
else
res = max(res, cur), cur = 1;
}
cout << res << endl;
return 0;
}
#include
using LL = long long;
using namespace std;
const int N = 1e4 + 7;
int n, a[N];
unordered_map<LL, LL> mp;
signed main() {
cin >> n;
while (n -- ) {
int a, b;
cin >> a >> b;
mp[a] ++, mp[b] ++;
}
LL res = 0;
for (auto [k, v] : mp) {
res = max(res, v);
}
cout << res << endl;
return 0;
}
#include
using LL = long long;
using namespace std;
typedef pair<int, string> PIS;
const int N = 1e4 + 7;
int n, a[N];
string b[N];
vector<PIS> v;
signed main() {
cin >> n;
for (int i = 0; i < n; i ++ ) {
cin >> a[i];
}
for (int i = 0; i < n; i ++ ) {
cin >> b[i];
}
for (int i = 0; i < n; i ++ ) {
v.emplace_back(a[i], b[i]);
}
sort(v.begin(), v.end(), [&](auto &a, auto &b) {
return a.first < b.first;
});
for (int i = 0; i < 3; i++) {
cout << v[i].second << endl;
}
return 0;
}
#include
using LL = long long;
using namespace std;
typedef pair<int, string> PIS;
const int N = 1e2 + 7;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int n, m;
int g[N][N];
void dfs(int x, int y) {
g[x][y] = 0;
for (int i = 0; i < 4; i++) {
int a = x + dx[i], b = y + dy[i];
if (a <= 0 || a > n || b <= 0 || b > m) continue;
if (g[a][b] == 1) dfs(a, b);
}
}
signed main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> g[i][j];
int res = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (g[i][j]) dfs(i, j), res ++;
cout << res << endl;
return 0;
}
#include
using namespace std;
const int N = 2e4 + 7;
int n, m, a[N], f[N];
signed main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
for (int j = m; j >= a[i]; j--) {
f[j] = max(f[j], f[j - a[i]] + a[i]);
}
}
cout << f[m] << endl;
return 0;
}
寒假结束后,小明回到了久违的实验室,结果……小明突然发现他忘记了他的密码。幸好,他曾经给自己留下了一些提示。
看起来,小明的提示是由两个字符串 S 1 S_1 S1和 S 2 S_2 S2组成,他依稀的记得,密码是由第一个字符串 S 1 S_1 S1,去掉第二个字符串 S 2 S_2 S2中所有出现过的字符得到的。
现在,小明想要你帮助他算出密码。
第一行包含一个字符串 S 1 S_1 S1,长度不超过 105,至少包含 1个字符。
第二行包含一个字符串 S 2 S_2 S2,长度不超过 105,至少包含 1个字符。
题目保证, S 1 S_1 S1 和 S 2 S_2 S2都只包含 A S C I I ASCII ASCII码 [ 32 , 126 ] [32,126] [32,126]的可见字符(即大小写字母、数字、标点符号和空格),且空格不会出现在字符串的首尾。
输出一个字符串,即小明的密码。题目保证,密码不为空或者全为空格。
This_is_not_my_password
_not_
Thisismypasswrd
=
NJUPT
njupt njupt njupt
#####样例输出:
NJUPT
a#b^c %d?e
abcdef
#^ %?
第一个样例中,S1 为 This_is_not_my_password
,S2 为 _not_
,去掉 S2
中的所有字符(_、n、o 和 t)
后,得到 Thisismypasswrd
。
对于 20% 的数据,S2 的长度不超过 1。
对于 40% 的数据, S 1 S_1 S1 和 S 2 S_2 S2的长度不超过10。
对于 60% 的数据, S 1 S_1 S1 和 S 2 S_2 S2仅由小写字母组成。
对于 80% 的数据, S 1 S_1 S1 和 S 2 S_2 S2不包括空格。
对于 100%的数据, S 1 S_1 S1 和 S 2 S_2 S2都只包含 A S C I I ASCII ASCII码 [ 32 , 126 ] [32,126] [32,126]的可见字符(即大小写字母、数字、标点符号和空格),长度不超过105,且空格不会出现在字符串的首尾。
#include
using namespace std;
const int N = 2e4 + 7;
int n, m;
unordered_map<char, int> mp;
signed main() {
string s1, s2, res;
getline(cin, s1);
getline(cin, s2);
for (auto &c : s2) {
mp[c] = 1;
}
for (auto &c : s1) {
if (mp.count(c)) continue;
res += c;
}
cout << res << endl;
return 0;
}
《扫雷》是一款单人或者多人的电脑游戏。游戏目标是找出所有没有地雷的方格,完成游戏;要是按了有地雷的方格,游戏失败。
小明是一个狂热的扫雷爱好者,可是自从换到了新电脑后,他发现新电脑没有装扫雷游戏。于是他决定自己写一个扫雷游戏。
小明发现,扫雷的游戏规则是这样的:
游戏开始于一个 n行m列的雷区,雷区中,一些格子含有隐藏的地雷(称之为地雷格),而其他格子不含地雷(称之为非地雷格)。当玩家翻开一个非地雷格时,该格将会出现一个数字——提示周围八个格子中有多少个是地雷格。玩家可以利用这些数字来推断哪些格子是地雷格,哪些格子是非地雷格。
现在,小明已经编写好了生成地雷格的程序,他想请你帮忙编写一个程序,来生成雷区中的非地雷数字格子,以此提示周围格子中有多少个地雷格。
第一行一三个整数 n, m和 k,表示扫雷棋盘的行数和列数和地雷总数,题目保证, 1 ≤ n , m ≤ 100 , 1 ≤ k ≤ n × m 1≤n,m≤100,1≤k≤n×m 1≤n,m≤100,1≤k≤n×m。接下来 k行,每行两个整数 c i 和 r i c_i和 r_i ci和ri,表示第 i个地雷格位于第 c i c_i ci列,第 r i r_i ri行。题目保证 ( 1 ≤ c i ≤ m , 1 ≤ r i ≤ n ) (1≤c_i≤m,1≤r_i≤n) (1≤ci≤m,1≤ri≤n)且地雷格坐标不重复。
输出包含n行,m列,表示雷区中的非地雷数字格子。如果一个格子是地雷格,则输出一个星号*;否则输出一个数字,表示周围格子中有多少个地雷格。
3 3 2
1 1
2 3
*10
221
1*1
2 3 2
2 1
1 2
2*1
*21
4 3 1
1 3
000
110
*10
110
对于 20% 的数据, k = 1 k=1 k=1。
对于 40%的数据, n = 1 n=1 n=1或 m = 1 m=1 m=1。
对于 60%的数据, n , m ≤ 10 n,m≤10 n,m≤10。
对于 80%的数据, n , m ≤ 50 n,m≤50 n,m≤50。
对于 100% 的数据, 1 ≤ n , m ≤ 100 , 1 ≤ k ≤ n × m 1≤n,m≤100,1≤k≤n×m 1≤n,m≤100,1≤k≤n×m。
#include
using namespace std;
const int N = 1e2 + 7;
int n, m, k;
char g[N][N];
signed main() {
cin >> n >> m >> k;
while (k -- ) {
int x, y;
cin >> y >> x;
g[x][y] = '*';
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (g[i][j] == '*') {
cout << g[i][j];
}
else {
int cnt = 0;
for (int dx = -1; dx <= 1; dx ++)
for (int dy = -1; dy <= 1; dy ++) {
int curx = i + dx, cury = j + dy;
if (curx >= 1 && curx <= n && cury >= 1 && cury <= m && g[curx][cury] == '*')
cnt ++;
}
cout << cnt;
}
}
cout << endl;
}
return 0;
}
在马尔代夫回程的飞机上,百无聊赖的小明和小红玩起了游戏。为了难倒小明,小红出了一个很复杂的问题。小红会给出一个正整数 n,小明则需要统计 [1,n] 中,满足下列条件的正整数的数目:该数小于或等于 n且各位数字之和为偶数,正整数的各位数字之和是其所有位上的对应数字相加的结果;该数是素数。若找不到满足上述条件的数,则答案为 0。小红觉得她赢定了,可是没想到,小明偷偷地把这个问题告诉了你,并且希望聪明的你能够发挥计算机的力量,通过编程解决这一问题。
输入包含一个正整数 n ( 1 ≤ n ≤ 105 ) n (1≤n≤105) n(1≤n≤105)。
输出一个整数,表示满足条件的正整数的数目。
4
1
30
5
对于第一个样例,满足条件的正整数为 2。只有 2和 4满足小于等于 4且各位数字之和为偶数,但是只有 2是素数。
对于第二个样例,满足条件的正整数为 2,11,13,17,19
。只有 14个整数满足小于等于 30 且各位数字之和为偶数,分别是: 2,4,6,8,11,13,15,17,19,20,22,24,26,28
,但是只有 2,11,13,17,19
是素数。
题目保证,对于 20%的数据, n ≤ 10 n≤10 n≤10。
题目保证,对于 40%的数据, n ≤ 100 n≤100 n≤100。
题目保证,对于 60% 的数据, n ≤ 1000 n≤1000 n≤1000。
题目保证,对于 80% 的数据, n ≤ 1 0 4 n≤10^4 n≤104。
题目保证,对于 100%的数据, n ≤ 1 0 5 n≤10^5 n≤105。
#include
using namespace std;
int n;
bool isprime(int x) {
if (x < 2) return false;
for (int i = 2; i <= x / i; i++) {
if (x % i == 0)
return false;
}
return true;
}
signed main() {
cin >> n;
int res = 0;
for (int i = 2; i <= n; i++) {
int x = i, s = 0;
while (x) {
s += x % 10;
x /= 10;
}
if (isprime(i) && s % 2 == 0) res ++ ;
}
cout << res << endl;
return 0;
}
载人航天是人类探索太空的重要方式之一。载人航天的目的是将宇航员送入太空,进行科学实验、技术验证、空间站建设等任务。载人航天的发展历程可以追溯到20世纪50年代,当时苏联和美国开始了载人航天的竞赛。1961年,苏联宇航员加加林成功地进行了第一次载人航天飞行。1969年,美国宇航员阿姆斯特朗成功地登上了月球。自此之后,载人航天技术不断发展,人类在太空中进行了大量的科学实验和技术验证。
为了尽可能的的延长宇航员在太空中的停留时间,食物是必不可少的,但是飞船的容量有限,仅能装载一部分食物箱上去。现在,宇航局握有所有食物箱的卡路里和质量,你需要帮助宇航局选择出一部分食物箱,使得它们的总质量不超过飞船的承载能力,同时总卡路里最大。
第一行两个整数 n 和 m,表示食物箱的数量和飞船的承载能力。题目保证,1≤n≤100, 1 ≤ m ≤ 1000 1≤m≤1000 1≤m≤1000。
接下来 n行,每行两个整数 wi 和 vi,表示第 i个食物箱的质量和卡路里。题目保证, 1 ≤ w i , v i ≤ 100 1≤w_i,v_i≤100 1≤wi,vi≤100。
输出一行一个整数,表示能够带上飞船的食物箱的最大总卡路里。
3 70
71 100
69 1
1 2
3
4 1
3 5
2 7
4 11
9 2
0
5 25
1 1
1 2
1 3
1 4
1 5
15
在第一组样例中,由于总负载不超过 70 ,所以可以选择第2和第3个食物箱,总卡路里为 1+2=3。
题目保证,对于 20%的数据点, ( ∑ n i = 1 w i ) ≤ m (∑n_i=1w_i)≤m (∑ni=1wi)≤m。
题目保证,对于 20%的数据点, n ≤ 3 n≤3 n≤3(类似样例1)。
题目保证,对于 60% 的数据点,数据中每一个食物箱的质量 w i w_i wi都是相同的(类似样例3)。
题目保证,对于 100 100% 100的数据点, 1 ≤ w i , v i ≤ 100 , 1 ≤ n ≤ 100 , 1 ≤ m ≤ 1000 1≤w_i,v_i≤100,1≤n≤100, 1≤m≤1000 1≤wi,vi≤100,1≤n≤100,1≤m≤1000。
#include
using namespace std;
const int N = 1e3 + 7;
int n, m;
int f[N];
signed main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int w, v;
cin >> w >> v;
for (int j = m; j >= w; j--) {
f[j] = max(f[j], f[j - w] + v);
}
}
cout << f[m] << endl;
return 0;
}