蓝桥杯小白赛第六期 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;
}
相关推荐
froyoisle3 分钟前
CSP 真题解析:[CSP-J 2020-T3] 表达式
c++·算法·csp·信息学·信奥赛
geats人山人海7 分钟前
算法好题 2026.7.18
算法
文祐16 分钟前
C++类之虚函数表没有虚继承的菱形继承
开发语言·c++·算法
Lumos18625 分钟前
51单片机从零到实战(8)——串口通信
算法
小帽子_1231 小时前
大功率 PCS 双环闭环控制算法原理与实现
算法
buhuizhiyuci2 小时前
【算法篇】位运算 —— 基础篇
java·数据库·算法
Black蜡笔小新2 小时前
算法训练完了怎么上线?DLTM→AIS→EasyGBS三步交付流水线
人工智能·算法
sycmancia2 小时前
Qt——多线程与界面组件的通信
开发语言·qt·算法
想吃火锅10053 小时前
【leetcode】5.最长回文子串js
算法·leetcode·职场和发展
CoderYanger3 小时前
A.每日一题:1967题+1331题
java·程序人生·算法·leetcode·面试·职场和发展·学习方法