Problem D: 简易占座
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 198 Solved: 42
[ Submit][ Status][ Web Board]
Description
新学期开始了,小明提早到自习教室帮同学占座,一本书可以占两个相邻座位,小明只想占一整排座位,求总共需要几本书来占满这一排空余座位?
Input
题目有多组测试数据
先输入一个数据n,表示这一排总共有多少个座位,再输入一行座位状态,"*"为不能占座,"@"为可以占座。
Output
输出需要几本书占座,如果没有座位可以来占座,则输出"Oh no!"
Sample Input
1 * 6 @**@@@
Sample Output
Oh no! 3
HINT#include <iostream>
#include <string>
using namespace std;
int main()
{
int n,sum=0;
string str;
cin>>n>>str;
for(int i=0;i<n;i++)
{
if(str[i] == '*') // if the current char is '*', we just move one step
continue;
else // if the current char is '@', we move two steps, no matter what the next char is
{
i++;
sum++;
}
}
if(sum == 0)
cout<<"Oh no!"<<endl;
else
cout<<sum<<endl;
return 0;
}
PS:唉!转载的!!!![难过](http://static.blog.csdn.net/xheditor/xheditor_emot/default/sad.gif)
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n,sum=0;
string str;
cin>>n>>str;
for(int i=0;i<n;i++)
{
if(str[i] == '*') // if the current char is '*', we just move one step
continue;
else // if the current char is '@', we move two steps, no matter what the next char is
{
i++;
sum++;
}
}
if(sum == 0)
cout<<"Oh no!"<<endl;
else
cout<<sum<<endl;
return 0;
}