【考研】南邮历年复试上机试题目与题解

【考研】南邮历年复试上机试题目与题解

文章目录

  • 【考研】南邮历年复试上机试题目与题解
    • 个人题目难度评估
    • 历年上机题目
      • 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 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;
}

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 ciri,表示第 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) (1cim,1rin)且地雷格坐标不重复。

输出:

输出包含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,vi100

输出:

输出一行一个整数,表示能够带上飞船的食物箱的最大总卡路里。

样例输入:
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,vi1001n1001m1000

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.mfbz.cn/a/781493.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【MySQL基础篇】多表查询

1、多表关系 概述&#xff1a;项目开发中&#xff0c;在进行数据库表结构操作设计时&#xff0c;会根据业务需求及业务模板之间的关系&#xff0c;分析并设计表结构&#xff0c;由于业务之间相互关联&#xff0c;所以各个表结构之间也存在着各种联系&#xff0c;基本上分为三种…

关于新装Centos7无法使用yum下载的解决办法

起因 之前也写了一篇类似的文章&#xff0c;但感觉有漏洞&#xff0c;这次想直接把漏洞补齐。 问题描述 在我们新装的Centos7中&#xff0c;如果想要用C编程&#xff0c;那就必须要用到yum下载&#xff0c;但是&#xff0c;很多新手&#xff0c;包括我使用yum下载就会遇到一…

WEB05Web开发HTMLCSS

Web前端开发 什么是 Web &#xff1f; Web&#xff1a;全球广域网&#xff0c;也称为万维网(www World Wide Web)&#xff0c;能够通过浏览器访问的网站。 Web 网站的工作流程 W3C 万维网联盟&#xff08; World Wide Web Consortium &#xff09;&#xff0c;创建于1994年1…

PD虚拟机不能复制Mac的文件怎么回事 PD虚拟机不能复制Mac的文件怎么办 Parallels Desktop怎么用

PD虚拟机不仅能提供跨系统协作的服务&#xff0c;还能进行虚拟机系统与原生系统间的文件共享、文本复制、文件复制等操作&#xff0c;让系统间的资源可以科学利用。但在实际操作过程中&#xff0c;PD虚拟机不能复制Mac的文件怎么回事&#xff1f;PD虚拟机不能复制Mac的文件怎么…

甘肃黄米粽子:香甜软糯的塞上美食

甘肃黄米粽子是甘肃地区具有特色的传统美食。黄米粽子选用优质的黄米作为主要原料&#xff0c;黄米相较于糯米&#xff0c;有着独特的谷物香气和口感。在制作过程中&#xff0c;将黄米浸泡一段时间&#xff0c;使其充分吸收水分&#xff0c;变得饱满。馅料方面&#xff0c;通常…

AcWing 1260:二叉树输出

【题目来源】https://www.acwing.com/problem/content/1262/【题目描述】 树的凹入表示法主要用于树的屏幕或打印输出&#xff0c;其表示的基本思想是兄弟间等长&#xff0c;一个结点的长度要不小于其子结点的长度。 二叉树也可以这样表示&#xff0c;假设叶结点的长度为 1&…

YOLOv8改进---BiFPN特征融合

一、BiFPN原理 1.1 基本原理 BiFPN&#xff08;Bidirectional Feature Pyramid Network&#xff09;&#xff0c;双向特征金字塔网络是一种高效的多尺度特征融合网络&#xff0c;其基本原理概括分为以下几点&#xff1a; 双向特征融合&#xff1a;BiFPN允许特征在自顶向下和自…

【驱动篇】龙芯LS2K0300之PWM设备驱动

实验目的 利用脉冲调制效应&#xff08;PWM&#xff09;等效改变输出功率大小控制LED&#xff0c;从而实现呼吸灯效果&#xff0c;需要用到RGB LED模块 模块连接 IO 插针接口上一共集成了两路PWM&#xff0c;分别是PWM2和PWM3&#xff0c;对应GPIO88、GPIO89 PWM2和PWM3对…

【Spring Cloud】一个例程快速了解网关Gateway的使用

Spring Cloud Gateway提供了一个在Spring生态系统之上构建的API网关&#xff0c;包括&#xff1a;Spring 5&#xff0c;Spring Boot 2和Project Reactor。Spring Cloud Gateway旨在提供一种简单而有效的路由方式&#xff0c;并为它们提供一些网关基本功能&#xff0c;例如&…

centos7.9 rpm包安装mysql8.2.0数据库、root设置客户端登录、配置并发、表名大小写敏感、启动重启指令等记录

centos安装mysql8数据库,下载的是rpm-bundle.tar包,这样可以在内网环境离线安装,工作中医院的服务器很多也是内网的,所以这里记录下rpm-bundle.tar包安装的步骤。 lscpu 查看处理器是x86还是arm 下载对应的版本 bundle tar包 ((mysql-8.2.0-1.el7.x86_64.rpm-bundle.tar))…

实验五 图像增强—空域滤波

一、实验目的 了解图像平滑滤波器&#xff08;均值滤波和中值滤波&#xff09;和图像锐化算子&#xff08;Sobel算子、Prewitt算子、Laplacian算子&#xff09;在工程领域中的应用&#xff1b;理解图像平滑滤波器和图像锐化算子的工程应用范围&#xff1b;掌握图像平滑滤波器和…

MSPM0G3507——编码器控制速度

绿色设置的为目标值100&#xff0c;红色为编码器实际数据 。 最后也是两者合在了一起&#xff0c;PID调试成功。 源码直接分享&#xff0c;用的是CCStheia&#xff0c;KEIL打不开。大家可以看一下源码的思路&#xff0c;PID部分几乎不用改 链接&#xff1a;https://pan.baid…

微信公众平台测试账号本地微信功能测试说明

使用场景 在本地测试微信登录功能时&#xff0c;因为微信需要可以互联网访问的域名接口&#xff0c;所以本地使用花生壳做内网穿透&#xff0c;将前端服务的端口和后端服务端口进行绑定&#xff0c;获得花生壳提供的两个外网域名。 微信测试账号入口 绑定回调接口 回调接口的…

C++左值右值

在C中&#xff0c;左值&#xff08;lvalue&#xff09;和右值&#xff08;rvalue&#xff09;是表达式分类的关键概念&#xff0c;它们主要影响表达式的赋值、函数调用以及操作符的使用方式。这些概念在C11及以后的版本中变得更加重要&#xff0c;因为引入了移动语义和右值引用…

字符串和正则表达式踩坑

// 中石化加油卡号格式&#xff1a;以 100011 开头共19位public static final String ZHONGSHIYOU_OIL_CARD_PATTERN "^100011\\d{13}$";// 中石油加油卡号格式&#xff1a;以90、95、70开头共16位public static final String ZHONGYOU_OIL_CARD_PATTERN "^(9…

按键控制LED流水灯模式定时器时钟

目录 1.定时器 2. STC89C52定时器资源 3.定时器框图 4. 定时器工作模式 5.中断系统 1&#xff09;介绍 2&#xff09;流程图&#xff1a;​编辑 3&#xff09;STC89C52中断资源 4&#xff09;定时器和中断系统 5&#xff09;定时器的相关寄存器 6.按键控制LED流水灯模…

去O化神器 Exbase

随着去O化进程推动&#xff0c;很多旧业务依赖的oracle数据库&#xff0c;都需要实现做数据库的替换&#xff0c;当下能很好兼容Oracle&#xff0c;并实现异构数据库之间转换的工具并不多。这里给大家推荐一个商业工具数据库迁移工具exbase&#xff08;北京海量&#xff09;&am…

谷粒商城学习笔记-17-快速开发-逆向工程搭建使用

文章目录 一&#xff0c;克隆人人开源的逆向工程代码二&#xff0c;把逆向工程集成到谷粒商城的后台工程三&#xff0c;以商品服务为例&#xff0c;使用逆向工程生成代码1&#xff0c;修改逆向工程的配置2&#xff0c;以Debug模式启动逆向工程3&#xff0c;使用逆向工程生成代码…

通信协议_C#实现自定义ModbusRTU主站

背景知识&#xff1a;modbus协议介绍 相关工具 mbslave:充当从站。虚拟串口工具:虚拟出一对串口。VS2022。 实现过程以及Demo 打开虚拟串口工具: 打开mbslave: 此处从站连接COM1口。 Demo实现 创建DLL库&#xff0c;创建ModbusRTU类,进行实现&#xff1a; using Syste…

OpenAI的崛起:从梦想到现实

OpenAI的崛起不仅是人工智能领域的重大事件&#xff0c;也是科技史上一个引人注目的篇章。本文将深入探讨OpenAI从创立到如今的演变过程&#xff0c;分析其成功的关键因素&#xff0c;以及未来的发展方向。 一、OpenAI的初创期&#xff1a;理想主义与混乱并存 OpenAI成立于20…