H_On个人小站

个人站

这都被你发现了【惊
奖励你一朵小红花~


【Codeforces Round#618 (Div. 2)】A. Non-zero 题解

题目链接:A. Non-zero

题目

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≤10^3). 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积不为 0 ,即数组里的任何一个数都不能为 0 。输出你需要的操作数。

解题思路

把数组里的 0 都加一变成 1 ,并且把所有数的总和加起来,最后加个判断,如果总和等于 0 的话,就随便让一个数加一即可,即让操作数加一就行了。

代码

#include <iostream>

using namespace std;

int main() {
        ios::sync_with_stdio(0); cin.tie(0);
        int t; cin >> t;
        while (t--) {
                int n; cin >> n;
                int s = 0, r = 0, z = 0;
                while (n--) {
                        int a; cin >> a;
                        s += a;
                        if (!a) r += 1, s += 1;
                }
                if (!s) r += 1;
                cout << r << endl;
        }
        return 0;
}