Title description
Since going to university, Susu has learned to skip class=. =! However, he still has choices when he skips classes, and he has to finish listening to certain courses when he is kneeling. Now, given the course sequence of a certain day, use 1 to indicate an important course and 0 to indicate a less important course. Susu always skips class according to the following rules:
- Never take important courses, never! ;
- All the lessons before the first important lesson are knocked out;
- If there are two or more non-important courses connected, all these non-important courses are knocked out;
- Knock out all the lessons after the last important course;
- If there are no important courses on the day, knock out all courses.
- Under the guidance of such an advanced thought of skipping classes, how many classes can Susu take every day?
enter:
First enter a positive integer T (T<=150), which means the number of groups to be tested. Each of the following test data contains two rows. The first line is a positive integer n (1<=n<=1000), indicating the total number of courses on a certain day, and the second line has n integers (only 0 or 1), indicating the importance of these n courses.
Output:
For each set of test data, output the number of lessons Su Suneng took.
Sample Input
2
5
0 1 0 1 1
7
1 0 1 0 0 1 0
Sample Output
Case #1:4
Case #2:4
Ideas
Skip the class according to the skipping rules given by the title, do not skip the class with the number 1; skip all classes before the first 1 appears; if there are two or more 0s between 1 and 1, then these 0s All classes are skipped; all classes after the last 1 class are skipped; if there is no class 1, all classes of the day are skipped.
C++ code:
#include<bits/stdc++.h>
using namespace std;
int n, t, num = 0, first, last, a[1005];
int main()
{
while(cin >> t)
{
for(int i = 0; i < t; i++)
{
cin >> n;
num = 0, first = -1, last = -1;
memset(a, 0, sizeof(a));
for(int j = 0; j < n; j++)
{
cin >> a[j];
if(a[j] == 1 && first == -1)
{
num++;
first = j;
last = j;
}
else if(a[j] == 1)
{
num++;
last = j;
}
}
for(int j = first; j < last; j++)
if(a[j] == 0&&a[j - 1] != 0 && a[j + 1] != 0)
num++;
cout << "Case #" << i + 1 << ":" << num << endl;
}
}
return 0;
}