CF300A Array 题解
时间:2022-12-01 17:00:00
思路很简单:
我们就先把正数,负数,零分别放入三个集合中:
stackzero; stackpo; stackne;
然后处理:
讨论的情况如下:
正数集为空,需要从负数集中抽取两个元素
负数集元素的数量为偶数,需要将奇数(这里抽一个)元素扔进0集中
处理后可以顺利进行AC啦~
#include using namespace std; int main() { stackzero; stackpo; stackne; int n;cin>>n; int k; int cnt0=0,cnt1=0,cnt2=0; while(n>0){ cin>>k; n--; if(k==0){zero.push(k);cnt2 ;} else if(k<0){ne.push(k);cnt0 ;} else if(k>0){po.push(k);cnt1 ;} }//结束 //处理 if(po.empty()){ po.push(ne.top());ne.pop(); po.push(ne.top());ne.pop()//将两个负数压入正集合 cnt0-=2; cnt1 =2; if(cnt0%2==0) {zero.push(ne.top());ne.pop();cnt0--;cnt2 ;} } if(cnt0%2==0) {zero.push(ne.top());ne.pop();cnt0--;cnt2 ;} cout<