0
点赞
收藏
分享

微信扫一扫

A. Non-zero(思维水题)Codeforces Round #618 (Div. 2)

原题链接: ​​https://codeforces.com/problemset/problem/1300/A​​​A. Non-zero(思维水题)Codeforces Round #618 (Div. 2)_自定义
测试样例:

input
4
3
2 -1 -1
4
-1 0 0 1
2
-1 2
3
0 -2 1
output
1
2
0
2

题意: 给你一个n长的序列,任务要求序列元素之和和元素之积都不为0,你可以进行一次操作将其中一个元素+1,问你完成至少任务至少要进行多少次操作?

解题思路: 水题一道,不愧为签到题,这道题我们只想让元素之和和元素之积不为0即可,我们可以统计元素之和,同时影响元素之积的在于元素值为0,所以我们也要统计元素值为0的数,那么我们关键就是要解决这些问题的最小操作:即元素中不存在0,元素之和为1。具体看代码。

AC代码:

/*

*
*/
#include<bits/stdc++.h> //POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int t,n,a[maxn];
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(cin>>t){
while(t--){
cin>>n;
int sum=0,cnt=0;
rep(i,1,n){
cin>>a[i];
sum+=a[i];
if(a[i]==0)cnt++;
}
if(sum==0){
if(cnt==0)
cout<<1<<endl;
else
cout<<cnt<<endl;
}
else{
if(cnt==0){
cout<<0<<endl;
}
else
if(sum+cnt==0){
cout<<cnt+1<<endl;
}
else
cout<<cnt<<endl;
}
}
}
return 0;
}


举报

相关推荐

0 条评论