Code Forces 586 A. Alena's Schedule(水~)

Description
一所学校一天有很多节课,该学校一女生每天要去上其中的几节课,如果有连续两节课及以上没课时她就会回家,否则就待学校,问该女生一天有多少节课待学校
Input
第一行为一整数n表示一天的所有课,第二行n个整数,1表示该女生这节课有课,0表示没课
Output
输出该女生一天要在学校待几节课
Sample Input
7
1 0 1 0 0 1 0
Sample Output
4
Solution
简单题~
Code

#include<cstdio>
#include<iostream>
using namespace std;
int n,a,t[111],ans,res;
int main()
{
    while(~scanf("%d",&n))
    {
        ans=res=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a);
            if(a) t[res++]=i;
        }
        if(res)ans++;;
        for(int i=1;i<res;i++)
            if(t[i]-t[i-1]>2)
                ans++;
            else
                ans+=t[i]-t[i-1];
        printf("%d\n",ans);
    }
    return 0;
} 

你可能感兴趣的:(Code Forces 586 A. Alena's Schedule(水~))