【考研】南郵歷年復試上機試題目與題解

【考研】南郵歷年復試上機試題目與題解

文章目錄

  • 【考研】南郵歷年復試上機試題目與題解
    • 個人題目難度評估
    • 歷年上機題目
      • PROB1002 求最值問題
      • PROB1003 新對稱素數問題
      • PROB1004 進制轉換
      • PROB1005 涂色問題 (待補)
      • PROB1006 最大公約數和最小公倍數
      • PROB1007 斐波那契數列
      • PROB1008 回文回文
      • PROB1009 單源最短路
      • PROB1010 萌萌摘蘋果
      • PROB1011 忠誠的騎士
      • PROB1012 最小質數合數之和問題
      • PROB1013 級數求和
      • PROB1014 小明與選擇題
      • PROB1017 小明喝可樂
      • PROB1018 華強種瓜
      • PROB1019 天子與諸侯
      • PROB1020 矩陣變換問題
      • PROB1021 小明的記憶游戲
      • PROB1022 小明算樹
      • PROB1023 小明的抽獎游戲
      • PROB1024 小明算分
      • PROB1025 開普勒星球歷法
      • PROB1026 子串計數
      • PROB1027 房屋裝修
      • PROB1028 最長連續上升子序列
      • PROB1029 社交網絡
      • PROB1029 極速狂飆
      • PROB1030 灰獵犬號
      • PROB1031 拿破侖傳 (背包)
    • 2024南京郵電大學上機復試:現場編程(第一場)
      • A. 密碼問題
        • 描述:
        • 輸入:
        • 輸出:
        • 樣例輸入:
        • 樣例輸出:
        • 樣例輸入:
        • 樣例輸入:
        • 樣例輸出:
        • 注釋:
        • AC Code
      • B. 掃雷分析器
        • 描述:
        • 輸入:
        • 輸出:
        • 樣例輸入:
        • 樣例輸出:
        • 樣例輸入:
        • 樣例輸出:
        • 樣例輸入:
        • 樣例輸出:
        • 注釋:
        • AC Code
    • 2024南京郵電大學上機復試:現場編程(第二場)
      • A. 數字游戲
        • 描述:
        • 輸入:
        • 輸出:
        • 樣例輸入:
        • 樣例輸出:
        • 樣例輸入:
        • 樣例輸出:
        • 注釋:
        • AC Code
      • B. 載人航天 (簡單背包變形,同ROB1038拿破侖傳)
        • 描述:
        • 輸入:
        • 輸出:
        • 樣例輸入:
        • 樣例輸出:
        • 樣例輸入:
        • 樣例輸出:
        • 樣例輸入:
        • 樣例輸出:
        • 注釋:
        • AC Code

個人題目難度評估

花了五個小時把題目整體做了一遍,現在平臺沒有判題機,不能測評,若有問題歡迎在評論區提出。題目整體難度不難,主要考察知識點:簡單模擬、結構體排序、簡單矩陣處理、字符串、STL應用、簡單數論(素數、公倍數等等)、進制轉換、日期問題、板子圖論(最短路、求連通塊)、裸背包問題、遞推
復試上機平臺:南郵復試上機平臺

歷年上機題目

PROB1002 求最值問題

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

PROB1003 新對稱素數問題

#include <iostream>
#include <algorithm>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;
}

PROB1004 進制轉換

#include <bits/stdc++.h>
#include <cstdlib>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;
}

PROB1005 涂色問題 (待補)

PROB1006 最大公約數和最小公倍數

#include <bits/stdc++.h>using namespace std;int main() {int a, b;cin >> a >> b;cout << __gcd(a, b) << " " << a * b / __gcd(a, b) << endl;return 0;
}

PROB1007 斐波那契數列

#include <bits/stdc++.h>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;
}

PROB1008 回文回文

#include <bits/stdc++.h>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;
}

PROB1009 單源最短路

#include <bits/stdc++.h>#define int long longusing 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() { // 堆優化dijkstrapriority_queue<PII, vector<PII>, greater<PII>> heap;heap.emplace(0, s); // dist - snomemset(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;
}

PROB1010 萌萌摘蘋果

#include <bits/stdc++.h>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;
}

PROB1011 忠誠的騎士

#include <bits/stdc++.h>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;
}

PROB1012 最小質數合數之和問題

#include <bits/stdc++.h>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;
}

PROB1013 級數求和

#include <bits/stdc++.h>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;
}

PROB1014 小明與選擇題

#include <bits/stdc++.h>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;
}

PROB1017 小明喝可樂

#include <bits/stdc++.h>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;
}

PROB1018 華強種瓜

#include <bits/stdc++.h>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;
}

PROB1019 天子與諸侯

#include <bits/stdc++.h>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;
}

PROB1020 矩陣變換問題

#include <bits/stdc++.h>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;
}

PROB1021 小明的記憶游戲

#include <bits/stdc++.h>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;
}

PROB1022 小明算樹

#include <bits/stdc++.h>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;
}

PROB1023 小明的抽獎游戲

#include <bits/stdc++.h>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;
}

PROB1024 小明算分

#include <bits/stdc++.h>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;
}

PROB1025 開普勒星球歷法

#include <bits/stdc++.h>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;
}

PROB1026 子串計數

#include <bits/stdc++.h>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;
}

PROB1027 房屋裝修

#include <bits/stdc++.h>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;
}

PROB1028 最長連續上升子序列

#include <bits/stdc++.h>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;
}

PROB1029 社交網絡

#include <bits/stdc++.h>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;
}

PROB1029 極速狂飆

#include <bits/stdc++.h>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;
}

PROB1030 灰獵犬號

#include <bits/stdc++.h>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;
}

PROB1031 拿破侖傳 (背包)

#include <bits/stdc++.h>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;
}

2024南京郵電大學上機復試:現場編程(第一場)

A. 密碼問題

描述:

寒假結束后,小明回到了久違的實驗室,結果……小明突然發現他忘記了他的密碼。幸好,他曾經給自己留下了一些提示。
看起來,小明的提示是由兩個字符串 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,且空格不會出現在字符串的首尾。

AC Code
#include <bits/stdc++.h>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;
}

B. 掃雷分析器

描述:

《掃雷》是一款單人或者多人的電腦游戲。游戲目標是找出所有沒有地雷的方格,完成游戲;要是按了有地雷的方格,游戲失敗。

小明是一個狂熱的掃雷愛好者,可是自從換到了新電腦后,他發現新電腦沒有裝掃雷游戲。于是他決定自己寫一個掃雷游戲🤣。

小明發現,掃雷的游戲規則是這樣的:

游戲開始于一個 n行m列的雷區,雷區中,一些格子含有隱藏的地雷(稱之為地雷格),而其他格子不含地雷(稱之為非地雷格)。當玩家翻開一個非地雷格時,該格將會出現一個數字——提示周圍八個格子中有多少個是地雷格。玩家可以利用這些數字來推斷哪些格子是地雷格,哪些格子是非地雷格。

現在,小明已經編寫好了生成地雷格的程序,他想請你幫忙編寫一個程序,來生成雷區中的非地雷數字格子,以此提示周圍格子中有多少個地雷格。

輸入:

第一行一三個整數 n, m和 k,表示掃雷棋盤的行數和列數和地雷總數,題目保證, 1 ≤ n , m ≤ 100 , 1 ≤ k ≤ n × m 1≤n,m≤100,1≤k≤n×m 1n,m1001kn×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) (1ci?m,1ri?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,m10
對于 80%的數據, n , m ≤ 50 n,m≤50 n,m50
對于 100% 的數據, 1 ≤ n , m ≤ 100 , 1 ≤ k ≤ n × m 1≤n,m≤100,1≤k≤n×m 1n,m1001kn×m

AC Code
#include <bits/stdc++.h>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;
}

2024南京郵電大學上機復試:現場編程(第二場)

A. 數字游戲

描述:

在馬爾代夫回程的飛機上,百無聊賴的小明和小紅玩起了游戲。為了難倒小明,小紅出了一個很復雜的問題。小紅會給出一個正整數 n,小明則需要統計 [1,n] 中,滿足下列條件的正整數的數目:該數小于或等于 n且各位數字之和為偶數,正整數的各位數字之和是其所有位上的對應數字相加的結果;該數是素數。若找不到滿足上述條件的數,則答案為 0。小紅覺得她贏定了,可是沒想到,小明偷偷地把這個問題告訴了你,并且希望聰明的你能夠發揮計算機的力量,通過編程解決這一問題。

輸入:

輸入包含一個正整數 n ( 1 ≤ n ≤ 105 ) n (1≤n≤105) n(1n105)

輸出:

輸出一個整數,表示滿足條件的正整數的數目。

樣例輸入:
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 n10
題目保證,對于 40%的數據, n ≤ 100 n≤100 n100
題目保證,對于 60% 的數據, n ≤ 1000 n≤1000 n1000
題目保證,對于 80% 的數據, n ≤ 1 0 4 n≤10^4 n104
題目保證,對于 100%的數據, n ≤ 1 0 5 n≤10^5 n105

AC Code
#include <bits/stdc++.h>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;
}

B. 載人航天 (簡單背包變形,同ROB1038拿破侖傳)

描述:

載人航天是人類探索太空的重要方式之一。載人航天的目的是將宇航員送入太空,進行科學實驗、技術驗證、空間站建設等任務。載人航天的發展歷程可以追溯到20世紀50年代,當時蘇聯和美國開始了載人航天的競賽。1961年,蘇聯宇航員加加林成功地進行了第一次載人航天飛行。1969年,美國宇航員阿姆斯特朗成功地登上了月球。自此之后,載人航天技術不斷發展,人類在太空中進行了大量的科學實驗和技術驗證。

為了盡可能的的延長宇航員在太空中的停留時間,食物是必不可少的,但是飛船的容量有限,僅能裝載一部分食物箱上去。現在,宇航局握有所有食物箱的卡路里和質量,你需要幫助宇航局選擇出一部分食物箱,使得它們的總質量不超過飛船的承載能力,同時總卡路里最大。

輸入:

第一行兩個整數 n 和 m,表示食物箱的數量和飛船的承載能力。題目保證,1≤n≤100, 1 ≤ m ≤ 1000 1≤m≤1000 1m1000

接下來 n行,每行兩個整數 wi 和 vi,表示第 i個食物箱的質量和卡路里。題目保證, 1 ≤ w i , v i ≤ 100 1≤w_i,v_i≤100 1wi?,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 n3(類似樣例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 1wi?,vi?1001n1001m1000

AC Code
#include <bits/stdc++.h>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;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/41264.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/41264.shtml
英文地址,請注明出處:http://en.pswp.cn/web/41264.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

解決Spring Boot中的數據庫連接池問題

解決Spring Boot中的數據庫連接池問題 大家好&#xff0c;我是微賺淘客系統3.0的小編&#xff0c;也是冬天不穿秋褲&#xff0c;天冷也要風度的程序猿&#xff01; 1. 理解數據庫連接池的重要性 數據庫連接池在任何使用數據庫的應用程序中都起著至關重要的作用。它們管理和維…

解析Java中的動態代理與靜態代理的區別

解析Java中的動態代理與靜態代理的區別 大家好&#xff0c;我是微賺淘客系統3.0的小編&#xff0c;也是冬天不穿秋褲&#xff0c;天冷也要風度的程序猿&#xff01; 1. 引言 代理模式是軟件開發中常用的一種設計模式&#xff0c;用于控制對其它對象的訪問。在Java中&#xf…

C#中的Task.Delay(2000).Wait() 與await Task.Delay(2000)

Task.Delay(2000).Wait() 和 await Task.Delay(2000) 在功能上看似相似&#xff0c;都用于等待一段時間&#xff08;在這個例子中是2000毫秒&#xff09;&#xff0c;但它們在使用方式和背后的行為上存在一些關鍵差異。 .Result 是 Task 類的一個屬性&#xff0c;它用于獲取任務…

算法刷題筆記 滑動窗口(C++實現,非常詳細)

文章目錄 題目描述基本思路實現代碼 題目描述 給定一個大小為n ≤ 10^6的數組。有一個大小為k的滑動窗口&#xff0c;它從數組的最左邊移動到最右邊。你只能在窗口中看到k個數字。每次滑動窗口向右移動一個位置。以下是一個例子&#xff1a; 該數組為 [1 3 -1 -3 5 3 6 7]&…

用HttpURLConnection復現http響應碼405

目錄 使用GET方法&#xff0c;訪問GET接口&#xff0c;服務端返回405使用GET方法&#xff0c;訪問POST接口&#xff0c;服務端返回405使用POST方法&#xff0c;訪問GET接口&#xff0c;服務端返回405 使用GET方法&#xff0c;訪問GET接口&#xff0c;服務端返回405 發生場景&a…

Linux shell編程學習筆記63:free命令 獲取內存使用信息

0 前言 在系統安全檢查中&#xff0c;內存使用情況也是一塊可以關注的內容。Linux提供了多個獲取內存信息的命令很多。今天我們先研究free命令。 1 free命令的功能、用法和選項說明 1.1 free命令的功能 free 命令可以顯示系統內存的使用情況&#xff0c;包括物理內存、交換…

Java多語言跨境電商外貿商城源碼 tiktok商城系統源碼 跨境電商源碼

Java多語言跨境電商外貿商城源碼 tiktok商城系統源碼 跨境電商源碼 技術棧 PC端使用&#xff1a;vueelementui 用戶端使用&#xff1a;uniapp 管理端使用&#xff1a;vueelementui 后臺服務使用&#xff1a;springbootmybatisplusmysql 功能描述&#xff1a; 對接PayPal…

【面試題】字節一面面試題

自我介紹&#xff0c;項目介紹MQ的使用場景&#xff0c;不同的MQ之前的區別&#xff0c;為什么使用公司的MQ數據庫怎么部署的&#xff08;應該是問節點&#xff0c;庫表&#xff09;事務隔離級別innodb為什么選可重復讀作為隔離級別數據庫三大日志&#xff0c;保存先后順序undo…

vue3+electron項目搭建,遇到的坑

我主要是寫后端,所以對前端的vue啊vue-cli只是知其然,不知其所以然 這樣也導致了我在開發前端時候遇到了很多的坑 第一個坑, vue2升級vue3始終升級不成功 第二個坑, vue add electron-builder一直卡進度,進度條走完就是不出提示succes 第一個坑的解決辦法: 按照網上說的升級v…

使用Java實現高性能的文件上傳下載服務

使用Java實現高性能的文件上傳下載服務 大家好&#xff0c;我是微賺淘客系統3.0的小編&#xff0c;也是冬天不穿秋褲&#xff0c;天冷也要風度的程序猿&#xff01; 1. 引言 在現代Web應用中&#xff0c;文件上傳和下載服務是非常常見的功能需求。如何實現高性能、可靠且安全…

Ubuntu 20.04下多版本CUDA的安裝與切換 超詳細教程

目錄 前言一、安裝 CUDA1.找到所需版本對應命令2.下載 .run 文件3.安裝 CUDA4.配置環境變量4.1 寫入環境變量4.2 軟連接 5.驗證安裝 二、安裝 cudnn1.下載 cudnn2.解壓文件3.替換文件4.驗證安裝 三、切換 CUDA 版本1.切換版本2.檢查版本 前言 當我們復現代碼時&#xff0c;總會…

深入分析SSL/TLS服務器的證書(C/C++代碼實現)

SSL&#xff08;Secure Sockets Layer&#xff09;和TLS&#xff08;Transport Layer Security&#xff09;是網絡安全領域的重要協議&#xff0c;它們在保護網絡通信中發揮著至關重要的作用。這些協議通過加密和身份驗證機制&#xff0c;確保數據在傳輸過程中的機密性和完整性…

建投數據與中再數科簽署戰略合作協議

近日&#xff0c;建投數據科技股份有限公司&#xff08;以下簡稱“建投數據”&#xff09;與中再保數字科技有限責任公司&#xff08;以下簡稱“中再數科”&#xff09;簽署戰略合作協議。雙方通過資源整合共享&#xff0c;實現優勢互補&#xff0c;共同探索產品及服務的跨領域…

初見:AntDB智能運維“三劍客“之ACC

前情回顧 在前兩個章節中&#xff0c;我們介紹了 AntDB 智能運維"三劍客"的 ADC 和 MTK。 初見&#xff1a;AntDB智能運維"三劍客"之ADC 初見&#xff1a;AntDB智能運維"三劍客"之MTK 本文將繼續介紹 AntDB 數據庫智能運維平臺 ACC。 AntDB 介紹…

如何設置PHP wkhtmltopdf

首先參考&#xff1a;Composer三步曲&#xff1a;安裝、使用、發布 在 php 路徑下&#xff0c;應能打開命令行輸入php -v能夠看到php版本信息。 然后執行以下三條&#xff1a; php -r "copy(https://install.phpcomposer.com/installer, composer-setup.php);"php…

minist數據集分類模型的訓練

minist數據集訓練 訓練方法&#xff1a;利用pytorch來實現minist數據集的分類模型訓練 訓練模型如下圖所示 模型代碼&#xff1a; import torch from torch import nn from torch.nn import Flattenclass Net(nn.Module):def __init__(self):super().__init__()self.module …

ChatGPT對話:Scratch編程中一個單詞,如balloon,每個字母行為一致,如何優化編程

【編者按】balloon 7個字母具有相同的行為&#xff0c;根據ChatGPT提供的方法&#xff0c;優化了代碼&#xff0c;方便代碼維護與復用。初學者可以使用7個字母精靈&#xff0c;復制代碼到不同精靈&#xff0c;也能完成這個功能&#xff0c;但不是優化方法&#xff0c;也沒有提高…

__builtin_constant_p 常量檢查函數

__builtin_constant_p 詳細介紹 功能&#xff1a;__builtin_constant_p 是 GCC (GNU Compiler Collection) 提供的一個內置函數&#xff0c;用于在編譯時檢測一個表達式是否是常量。它返回一個整型值&#xff1a; 如果表達式 exp 是編譯時常量&#xff0c;則返回 1。否則&…

【sklearn模型訓練全指南】深入理解機器學習模型的構建過程

標題&#xff1a;【sklearn模型訓練全指南】深入理解機器學習模型的構建過程 在機器學習中&#xff0c;模型訓練是一個核心過程&#xff0c;它涉及到從數據中學習并獲得預測能力。scikit-learn&#xff08;簡稱sklearn&#xff09;作為Python中一個廣泛使用的機器學習庫&#…

FairJob:促進在線廣告系統公平性研究

在人工智能&#xff08;AI&#xff09;與人類動態的交匯處&#xff0c;既存在機遇也存在挑戰&#xff0c;特別是在人工智能領域。盡管取得了進步&#xff0c;但根植于歷史不平等中的持續偏見仍然滲透在我們的數據驅動系統中&#xff0c;這些偏見不僅延續了不公平現象&#xff0…