蓝桥杯小白赛第六期 3.数学奇才

3.数学奇才【算法赛】 - 蓝桥云课 (lanqiao.cn)

这是我的代码,过了样例:

js 复制代码
#include<iostream>
#include<vector>
using namespace std;
typedef long long LL;
const int N = 1e9 + 5;
const LL T = -1e9 + 10;
LL sum = 0;
vector<LL> temp;
int cnt = 0;
int flag = 0;
int max_n = -1;
int a[100000005];

int main() {
    int n; cin >> n;

    for (int i = 0; i <n; i++)cin >> a[i];

    for (int i = 0; i < n; i++) {
        if (a[i] < 0) {
            cnt++;
        }
        else if (a[i] > 0) {
            temp.push_back(cnt);
            cnt = 0;
        }
    }

    if (temp.size() > 0) { // 确保 temp 不为空
        int index = 0;
        for (int i = 0; i < temp.size(); i++) {
            if (temp[i] > max_n) {
                max_n = temp[i];
            }
        }


        for (int i = 0; i < max_n; i++)
        {
            a[i] *= -1;
        }


        for (auto& it :a)sum += it;
    }

    cout << sum << endl;
    return 0;
}

这是人家的代码:

js 复制代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n; cin >> n;
    vector<ll> a(n);
    ll sum=0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        sum+=abs(a[i]);
    }
    cout<<sum<<endl;
    return 0;
}
相关推荐
2301_764441333 分钟前
新能源汽车电磁辐射高级预测
python·算法·数学建模·汽车
Keep_Trying_Go9 分钟前
论文Leveraging Unlabeled Data for Crowd Counting by Learning to Rank算法详解
人工智能·pytorch·深度学习·算法·人群计数
仟濹27 分钟前
【C/C++】经典高精度算法 5道题 加减乘除「复习」
c语言·c++·算法
顾安r43 分钟前
11.21 脚本 网页优化
linux·前端·javascript·算法·html
WolfGang0073211 小时前
代码随想录算法训练营Day27 | 56.合并区间、738.单调递增的数字、968.监控二叉树
算法
信奥卷王2 小时前
2025年9月GESPC++三级真题解析(含视频)
开发语言·c++·算法
努力学习的小廉2 小时前
我爱学算法之—— BFS之FLoodFill算法
算法·宽度优先
天选之女wow2 小时前
【Hard——Day8】65.有效数字、68.文本左右对齐、76.最小覆盖子串
linux·运维·redis·算法·leetcode
AI大模型学徒2 小时前
NLP基础(八)_马尔可夫模型
算法·机器学习·自然语言处理·nlp·概率论·马尔可夫模型
前端小L3 小时前
图论专题(十八):“逆向”拓扑排序——寻找图中的「最终安全状态」
数据结构·算法·安全·深度优先·图论·宽度优先