A. Alena's Schedule
2015-10-18 19:13
295 查看
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
output
input
output
input
output
Note
In the first sample Alena stays at the university from the second to the fifth pair, inclusive, during the third pair she will be it the university waiting for the next pair.
In the last sample Alena doesn't have a single pair, so she spends all the time at home.
解题说明:此题是一道模拟题,只需要找到不连续出现两个0的情况进行统计。
#include<stdio.h>
#include <string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n,i,s=0;
int a[102];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]==1)
{
s++;
}
}
for(i=0;i<n;i++)
{
if(a[i]==1 && a[i+1]==0 && a[i+2]==1)
{
s++;
}
}
printf("%d\n",s);
return 0;
}
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
5 0 1 0 1 1
output
4
input
7 1 0 1 0 0 1 0
output
4
input
1 0
output
0
Note
In the first sample Alena stays at the university from the second to the fifth pair, inclusive, during the third pair she will be it the university waiting for the next pair.
In the last sample Alena doesn't have a single pair, so she spends all the time at home.
解题说明:此题是一道模拟题,只需要找到不连续出现两个0的情况进行统计。
#include<stdio.h>
#include <string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n,i,s=0;
int a[102];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]==1)
{
s++;
}
}
for(i=0;i<n;i++)
{
if(a[i]==1 && a[i+1]==0 && a[i+2]==1)
{
s++;
}
}
printf("%d\n",s);
return 0;
}
相关文章推荐
- 软件测试过程模型的种类之----------V模型
- Dijkstra-POJ-1847-Tram
- 设计一个算法把一个十进制整数转化为二至九进制之间的任意进制数输出
- iOS百度地图定位功能iOS8+
- SQL中char、varchar、nvarchar的区别
- 10进制转8进制 ;内附所有进制转换
- lintcode 容易题:Partition List 链表划分
- [转]CocoaPods安装和使用教程
- HTML学习笔记之表格的使用
- 1041. 考试座位号(15)
- Ubuntu上的Read from socket failed: Connection reset by peer
- 黑马程序员——Java中的常用类总结
- 第七周 项目一-顺序环形队列算法库
- 截取utf8字符串
- node+mongodb+WP构建的移动社交应用源码 分享
- 测试数据库sql声明效率
- STL中 map 和 multimap
- Fragment之间的通信交互问题
- hdu5504 GT and sequence
- 黑马程序员——集合之Set和Map