这代码也能过???样例出错了吧???
查看原帖
这代码也能过???样例出错了吧???
755293
vegetable_doggy楼主2023/11/5 22:09
#include<iostream>
using namespace std;
int n,a,b,x;
int main(){
    cin>>n;
    while(n--)cin>>x,(x>0)&&(a++),(x<0)&&(b++);
    while(a&&b)cout<<1<<' '<<-1<<' ',a--,b--;
    while(a)cout<<1<<' ',a--;
    while(b)cout<<-1<<' ',b--;
    return 0;
}

对于此代码

输入

4 
1 1 -1 -1
输出
1 -1 1 -1
最大字段和为2
然而若将该序列重排为
1 -1 -1 1
最大字段和显然为1
谁能解释一下???
2023/11/5 22:09
加载中...