蓝桥杯小白赛第六期 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;
}
相关推荐
做怪小疯子21 分钟前
LeetCode 热题 100——双指针——三数之和
算法·leetcode·职场和发展
高山上有一只小老虎29 分钟前
等差数列前n项的和
java·算法
sin_hielo37 分钟前
leetcode 2536
数据结构·算法·leetcode
flashlight_hi43 分钟前
LeetCode 分类刷题:203. 移除链表元素
算法·leetcode·链表
py有趣43 分钟前
LeetCode算法学习之数组中的第K个最大元素
学习·算法·leetcode
吗~喽43 分钟前
【LeetCode】将 x 减到 0 的最小操作数
算法·leetcode
what_20181 小时前
list集合使用
数据结构·算法·list
hetao17338372 小时前
2025-11-13~14 hetao1733837的刷题记录
c++·算法
hansang_IR2 小时前
【题解】洛谷 P2476 [SCOI2008] 着色方案 [记搜]
c++·算法·记忆化搜索
趙卋傑2 小时前
常见排序算法
java·算法·排序算法