跳到正文

题解

题解:CF2218B The 67th 6-7 Integer Problem

Zhu_tian_yu约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年4月11日
收录
2026年5月24日
更新
2026年11月9日
篇幅
约 3 分钟
题目
CF2218B

版本

  1. v1当前

题目大意

给定 7 个数,将其中的 6 个取反,输出和的最大值。

思路

1

找出最大值,将所有数取反相加,加上两倍最大值。

Code
C++
#include<bits/stdc++.h>
using namespace std;
int n=0,a,s=0,maxn=-2147483647;
int main(){
    cin>>n;
    while(n--){
        s=0,maxn=-2147483647;
        for(int i=0;i<7;i++){
            cin>>a;
            s-=a;
            maxn=max(maxn,a);
        }
        cout<<s+2*maxn<<endl;
    }
}
AC 记录

2

暴力枚举,计算所有可能,取最大值。

Code
C++
#include<bits/stdc++.h>
using namespace std;
int n=0,a[7],s=0,maxn=-2147483647;
int main(){
    cin>>n;
    while(n--){
        s=0,maxn=-2147483647;
        for(int i=0;i<7;i++){
            cin>>a[i];
            s-=a[i];
        }
        for(int i=0;i<7;i++){
            maxn=max(maxn,s+2*a[i]);
        }
        cout<<maxn<<endl;
    }
}
AC 记录

3

对输入数据进行排序,用最大值减去其余值。

Code
C++
#include<bits/stdc++.h>
using namespace std;
int n=0,a[7],s=0;
int main(){
    cin>>n;
    while(n--){
        s=0;
        for(int i=0;i<7;i++){
            cin>>a[i];
        }
        sort(a,a+7);
        for(int i=0;i<6;i++){
            s-=a[i];
        }
        s+=a[6];
        cout<<s<<endl;
    }
}
AC 记录