第五次CCF计算机软件能力认证

1、数列分段

ACwing 3217

#include <iostream>
#include <algorithm>
using namespace std;

const int N = 1010;

int n, a[N];

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    int res = 1;
    int val = a[0];
    for (int i = 1; i < n; i++)
        if (a[i] == val) continue;
        else val = a[i], res++;
    cout << res << endl;
    return 0;
}

2、日期计算

ACwing 3218

#include <iostream>
#include <algorithm>
using namespace std;

const int month[13] = {0, 31, 28, 31, 30, 31, 30,
                       31, 31, 30, 31, 30, 31};

int is_leap(int y) {
    if (y % 4 == 0 && y % 100 || y % 400 == 0) return 1;
    return 0;
}

int get_month_day(int y, int m) {
    if (m == 2) return month[m] + is_leap(y);
    return month[m];
}

int main() {
    int y, s; cin >> y >> s;
    int m = 1, d = 1; s--;
    while (s--)
        if (++d > get_month_day(y, m)) {
            d = 1, m++;
            if (m > 12)
                m = 1, y++;
        }
    cout << m << endl << d << endl;
    return 0;
}

3、模板生成系统

ACwing 3219

#include <iostream>
#include <algorithm>
#include <cstring>
#include <unordered_map>
#include <vector>
using namespace std;

int n, m;
vector<string> strs;
unordered_map<string, string> vars;

int main() {
    cin >> n >> m; getchar();
    while (n--) {
        string str; getline(cin, str);
        strs.push_back(str);
    }
    while (m--) {
        string key, value; cin >> key;
        char c;
        while (c = getchar(), c != '\"');
        while (c = getchar(), c != '\"') value += c;
        vars[key] = value;
    }
    for (auto &str: strs) {
        for (int i = 0; i < str.size();)
            if (i + 1 < str.size() && str[i] == '{' && str[i + 1] == '{') {
                int j = i + 3;
                string key;
                while (str[j] != ' ' || str[j + 1] != '}' || str[j + 2] != '}')
                    key += str[j++];
                cout << vars[key];
                i = j + 3;
            } else cout << str[i++];
        puts("");
    }
    return 0;
}

4、高速公路(强连通分量)

ACwing 3220

如果图中 A A A 能够走到 B B B 并且 B B B 也能够走到 A A A,那么两点一定在同一个强连通分量中。所以可以先求出图中所有的强连通分量,对于每一个强连通分量,如果内部存在 n n n 个点,那么这个强连通分量中可以相互到达的点的数量为 C n 2 C_n^2 Cn2

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 10010, M = 100010;

int n, m;
int h[N], e[M], ne[M], idx;
int dfn[N], low[N];
int stk[N], top, ts;
bool in_stk[N];
int ans;

void add(int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

void tarjan(int u) {
    dfn[u] = low[u] = ++ts;
    stk[++top] = u, in_stk[u] = true;
    for (int i = h[u]; ~i; i = ne[i]) {
        int j = e[i];
        if (!dfn[j]) {
            tarjan(j);
            low[u] = min(low[u], low[j]);
        } else if (in_stk[j]) low[u] = min(low[u], dfn[j]);
    }

    if (dfn[u] == low[u]) {
        int y, cnt = 0;
        do {
            y = stk[top--];
            in_stk[y] = false;
            cnt++;
        } while (y != u);
        ans += cnt * (cnt - 1) / 2;
    }
}

int main() {
    scanf("%d%d", &n, &m);
    memset(h, -1, sizeof h);
    while (m--) {
        int a, b;
        scanf("%d%d", &a, &b);
        add(a, b);
    }

    for (int i = 1; i <= n; i++)
        if (!dfn[i])
            tarjan(i);
    printf("%d\n", ans);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值