Guy-Manuel and Thomas have an array aa of nn integers [a1,a2,…,an ]. In one step they can add 11 to any element of the array. Formally, in one step they can choose any integer index ii (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+an≠0 and a1⋅a2⋅an≠0 .
Each test contains multiple test cases.
The first line contains the number of test cases tt (1≤t≤103). The description of the test cases follows.
The first line of each test case contains an integer nn (1≤n≤100) — the size of the array.
The second line of each test case contains nn 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
水题。首先起码要满足每个数都不等于0,遍历时遇到为0的数先对其+1,同时ans++(进行了一次操作),同时sum(数列和)加上更新后的数(如果没更新就加上原数)。遍历完成后如果sum等于0的话,可以随便找一个数+1,同时ans++,这样能满足操作最少且sum不为0。最后输出ans即可。
#include <bits/stdc++.h> using namespace std; int t; int main() { cin>>t; while(t--) { int n; cin>>n; long long ans=0; int i;int temp; long long sum=0; for(i=1;i<=n;i++) { scanf("%d",&temp); if(temp==0) { temp++; ans++; } sum+=temp; } if(sum==0)ans++; cout<<ans<<endl; } }