蓝桥杯小白赛第六期 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;
}
相关推荐
独自破碎E7 分钟前
【总和拆分 + 双变量遍历】LCR_012_寻找数组的中心下标
数据结构·算法
WBluuue7 分钟前
Codeforces 1076 Div3(ABCDEFG)
c++·算法
u01092727118 分钟前
模板编译期排序算法
开发语言·c++·算法
GIS瞧葩菜27 分钟前
Cesium 轴拖拽 + 旋转圈拖拽 核心数学知识
人工智能·算法·机器学习
m0_6860416134 分钟前
C++中的适配器模式变体
开发语言·c++·算法
txzrxz34 分钟前
结构体排序,双指针,单调栈
数据结构·算法·双指针算法·单调栈·结构体排序
AndrewHZ38 分钟前
【AI黑话日日新】什么是AI智能体?
人工智能·算法·语言模型·大模型·llm·ai智能体
wWYy.39 分钟前
算法:二叉树最大路径和
数据结构·算法
葱明撅腚41 分钟前
利用Python挖掘城市数据
python·算法·gis·聚类
We་ct44 分钟前
LeetCode 36. 有效的数独:Set实现哈希表最优解
前端·算法·leetcode·typescript·散列表