zoukankan      html  css  js  c++  java
  • Codeforces Round #618 (Div. 2)A. Non-zero

    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+ +an0+an≠0 and a1a2⋅an≠0 .

    Input

    Each test contains multiple test cases.

    The first line contains the number of test cases tt (1t103). The description of the test cases follows.

    The first line of each test case contains an integer nn (1n100) — the size of the array.

    The second line of each test case contains nn integers a1,a2,,an (100ai100 ) — 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 <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;    
        }
    }
  • 相关阅读:
    【20211012】以确定去适应不确定
    【20211011】工作之余
    【20211009】二孩的心思
    【20211010】幸福声响
    【20211007】连岳摘抄
    【读书笔记】《创新与企业家精神》
    【20211006】连岳摘抄
    【20211008】生活规律,事情也好解决
    ASP.NET中DataGrid的简单用法
    无限级分类的实现
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12290682.html
Copyright © 2011-2022 走看看