codeforces586A Alena's Schedule(模拟)

来源:互联网 发布:淘宝浅草风是正品吗 编辑:程序博客网 时间:2024/06/05 09:54

题目链接

http://codeforces.com/problemset/problem/586/A

A. Alena's Schedule
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Alena has successfully passed the entrance exams to the university and is now looking forward to start studying.

One two-hour lesson at the Russian university is traditionally called a pair, it lasts for two academic hours (an academic hour is equal to 45 minutes).

The University works in such a way that every day it holds exactly n lessons. Depending on the schedule of a particular group of students, on a given day, some pairs may actually contain classes, but some may be empty (such pairs are called breaks).

The official website of the university has already published the schedule for tomorrow for Alena's group. Thus, for each of the n pairs she knows if there will be a class at that time or not.

Alena's House is far from the university, so if there are breaks, she doesn't always go home. Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university.

Of course, Alena does not want to be sleepy during pairs, so she will sleep as long as possible, and will only come to the first pair that is presented in her schedule. Similarly, if there are no more pairs, then Alena immediately goes home.

Alena appreciates the time spent at home, so she always goes home when it is possible, and returns to the university only at the beginning of the next pair. Help Alena determine for how many pairs she will stay at the university. Note that during some pairs Alena may be at the university waiting for the upcoming pair.

Input

The first line of the input contains a positive integer n (1 ≤ n ≤ 100) — the number of lessons at the university.

The second line contains n numbers ai (0 ≤ ai ≤ 1). Number ai equals 0, if Alena doesn't have the i-th pairs, otherwise it is equal to 1. Numbers a1, a2, ..., an are separated by spaces.

Output

Print a single number — the number of pairs during which Alena stays at the university.

Sample test(s)
input
50 1 0 1 1
output
4
input
71 0 1 0 0 1 0
output
4
input
10
output
0
题目比较水 题意是给你N节课,1表示上课 0表示这节没课,主人公在两节连着没课的情况下就会回家,如果两节课中间只有一节没课就不回家。问你主人公要在学校待多少课时。

#include<stdio.h>#include<algorithm>#include<cstring>#include<iostream>using namespace std;int main(){    int n;    int sum,ans,i,a[110],flag,k,j;    flag=0;    sum=0;    memset(a,0,sizeof(a));    scanf("%d",&n);    for(i=1;i<=n;i++)    {        scanf("%d",&a[i]);        if(flag==0&&a[i]==1)        {            flag=1;            k=i;        }    }    if(flag==0) printf("0\n");    else{        for(i=k;i<=n;i++)        {            if(a[i]==1)            {                sum++;                if(a[i+1]==0&&a[i+2]==0)                {                    for(j=i+1;j<=n;j++)                    {                        if(a[j]!=0)                        {                            i=j-1;                            break;                        }                    }                }            }            else{                if(a[i+1]==0)                {                    for(j=i+1;j<=n;j++)                    {                        if(a[j]!=0)                        {                            i=j-1;                            break;                        }                    }                }                else sum++;            }        }        printf("%d\n",sum);    }}






0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 荣耀手机一直在开机画面怎么办 玻璃手机壳碎了怎么办 华为6x信号不好怎么办 昂达平板v819i刷成砖了怎么办 华为5x忘记密码怎么办 荣耀8开不了机怎么办 华为5s死机了怎么办 华为重启后忘了解锁密码怎么办 华为mate.9上网速度慢怎么办 华为mate10上网速度慢怎么办 四核豌豆2变砖怎么办 苹果32g不够用怎么办 移动4g网络卡怎么办 移动4g网非常卡怎么办 移动4g卡网速慢怎么办 手机移动4g网卡怎么办 华为手机摄像头进灰怎么办 美图t8忘记密码怎么办 华为mate7忘记开机密码怎么办 华为mate9开机密码忘记怎么办 华为手机系统更新失败怎么办 mate9系统升级后耗电快怎么办 华为荣耀手机耗电快怎么办 荣耀10手机拍照不清晰怎么办 荣耀6x拍照模糊怎么办 华为荣耀P9进水了怎么办 苹果5s手机信号不好怎么办 苹果5s信号不好怎么办? 苹果5s像素不好怎么办 华为mate边缘有缝怎么办 华为mate8边缘有缝怎么办 手机系统占用运行内存大怎么办 安卓手机储存空间不足怎么办 手机显示系统空间不足怎么办 联系人存储已停止运行怎么办 荣耀note8听筒声音小怎么办 红米4x内存不足怎么办 安卓电视内存小怎么办 红米手机运行内存不足怎么办 红米手机存储空间不足怎么办 华为手机储存空间不足怎么办