题目描述:
Guy-Manuel and Thomas have an array a of n integers [a1,a2,…,an]. In one step they can add 1 to any element of the array. Formally, in one step they can choose any integer index i (1≤i≤n) and do ai:=ai+1.
If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.
What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make a1+a2+ … +an≠0 and a1⋅a2⋅ … ⋅an≠0.
输入描述:
Each test contains multiple test cases.
The first line contains the number of test cases t (1≤t≤103). The description of the test cases follows.
The first line of each test case contains an integer n (1≤n≤100) — the size of the array.
The second line of each test case contains n integers a1,a2,…,an (−100≤ai≤100) — elements of the array .
输出描述:
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.
输入:
4
3
2 -1 -1
4
-1 0 0 1
2
-1 2
3
0 -2 1
输出:
1
2
0
2
题意:
给定一个长度为n的整数数组,我们的任务是:经过若干次操作让这个数组的元素总和、元素积均不为0.
唯一允许的操作是:选定数组某个元素,把他+1.
现在你需要求出,完成该任务最少的操作次数。
题解:
直接搞
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = 1000 + 5;
int a[maxn];
int main(){
int t,n;
scanf("%d",&t);
while(t--){
int cnt = 0;
scanf("%d",&n);
for(int i = 1; i <= n; i ++) scanf("%d",&a[i]);
sort(a + 1,a + n + 1);
int sum = 0;
for(int i = 1; i <= n; i ++){
if(a[i] == 0) cnt ++;
sum += a[i];
}
if(sum && !cnt) printf("0\n");
else{
int ans = 0;
if(cnt){
ans = cnt;
sum += cnt;
}
if(!sum) ans ++;
printf("%d\n",ans);
}
}
return 0;
}