蓝桥杯小白赛第六期 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;
}
相关推荐
hi0_63 分钟前
03 数组 VS 链表
java·数据结构·c++·笔记·算法·链表
aPurpleBerry4 分钟前
hot100 hot75 栈、队列题目思路
javascript·算法
卷福同学1 小时前
【AI编程】AI+高德MCP不到10分钟搞定上海三日游
人工智能·算法·程序员
mit6.8241 小时前
[Leetcode] 预处理 | 多叉树bfs | 格雷编码 | static_cast | 矩阵对角线
算法
皮卡蛋炒饭.2 小时前
数据结构—排序
数据结构·算法·排序算法
??tobenewyorker3 小时前
力扣打卡第23天 二叉搜索树中的众数
数据结构·算法·leetcode
贝塔西塔3 小时前
一文读懂动态规划:多种经典问题和思路
算法·leetcode·动态规划
众链网络3 小时前
AI进化论08:机器学习的崛起——数据和算法的“二人转”,AI“闷声发大财”
人工智能·算法·机器学习
4 小时前
Unity开发中常用的洗牌算法
java·算法·unity·游戏引擎·游戏开发
飒飒真编程5 小时前
C++类模板继承部分知识及测试代码
开发语言·c++·算法