蓝桥杯小白赛第六期 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;
}
相关推荐
近津薪荼12 分钟前
优选算法——分治(2):快速选择
算法
Z...........12 分钟前
(优选算法)斐波那契数列模型
数据结构·算法
zyjyyds11329 分钟前
和为0的四元组-双指针法(C语言实现)
c语言·数据结构·算法
炽烈小老头1 小时前
【每天学习一点算法 2026/03/16】电话号码的字母组合
学习·算法
Lee川1 小时前
时空迷宫探险记:从O(1)到O(2^n)的算法进化论
算法·面试
KangJX1 小时前
Matrix获取卡顿堆栈 (Point Stack)
算法·客户端
靠沿1 小时前
【优选算法】专题十三——队列+宽搜(BFS)
算法·宽度优先
ccLianLian2 小时前
算法·字符串哈希
算法·哈希算法
SongYuLong的博客2 小时前
Linux IPC进程通信几种方法
linux·运维·算法
像污秽一样2 小时前
算法设计与分析-习题6.1
数据结构·算法