Home > Back-end >  Without thinking, to solve
Without thinking, to solve

Time:10-07

The question
A contains n is given a number of sequences, which can be any number plus one, in a sequence of operation asked at least this many times to let each number and sequence and not to 0,

The input description
How much each test set of test data,
The first line contains a number of test data t, behind that shows how many set of sample,
The behavior of each sample input an integer n, the sequence is the number of a element,
Then the second line contains n digits, respectively, a1, a2... The an

The output shows that
For each sample, the output with the least number of operations

CodePudding user response:

Thinking: traversal sequence, the number of elements in the statistics of 0 CNT (because of 0 + 1 operation must be), and the total sum of the sequence, and then determine whether the number of 0 + CNT total sum is 0, if not zero, then CNT times is ok, otherwise, will be for CNT + 1 times
 int main () {
Int t, n, I, j, CNT, sum=0;
Cin> t;
While (t -) {
Cin> n;
CNT=0;
for(i=0; iCin> j;
The sum +=j;
If (j==0) cnt++;//how many elements is zero, how many times will be operating + 1
}
If (sum + CNT==0) cnt++;//if the sum after all the elements of 0 + 1 to 0, for any element 0 before an additional + 1
operation at aCout}
return 0;
}
  • Related