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

19 篇文章 0 订阅

题目链接: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;
}

请多多支持猹的个人博客 H_On 个人小站 啊啊啊~拜托惹 > ~ <
因为猹的小站真的还挺可的,所以那边更新的也比较勤奋,感谢关注~我会努力的(ง •_•)ง

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值