Codeforces Round #618 (Div. 2)A. Non-zero

3477 단어
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 .
Input
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 .
Output
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.
Example
Input
 
4
3
2 -1 -1
4
-1 0 0 1
2
-1 2
3
0 -2 1

Output
 
1
2
0
2

。 0, 0 +1, ans++( ), sum( ) ( )。 sum 0 , +1, ans++, sum 0。 ans 。
#include 
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<endl;    
    }
}

좋은 웹페이지 즐겨찾기