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+ …… +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; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!