首页 新闻 会员 周边

大神们,速来围观一下这道c++编程题,求解。。。

0
悬赏园豆:5 [已解决问题] 解决于 2014-03-12 14:56

我知道,对于那些经验丰富的编程人员,这道题很简单。但是我c++刚入门。请神人们耐心帮一下!!!在此谢过。。。。

 

Description
新学期开始了,小明提早到自习教室帮同学占座,一本书可以占两个相邻座位,小明只想占一整排座位,求总共需要几本书来占满这一排空余座位?

Input
题目有多组测试数据

先输入一个数据n,表示这一排总共有多少个座位,再输入一行座位状态,"*"为不能占座,"@"为可以占座。

Output
输出需要几本书占座,如果没有座位可以来占座,则输出"Oh no!"

Sample Input
1
*
6
@**@@@
Sample Output
Oh no!

3

张慧慧的主页 张慧慧 | 初学一级 | 园豆:149
提问于:2014-03-01 17:45
< >
分享
最佳答案
0
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int numberOfSeat, numberOfBook = 0;
 8     char seats[50];
 9     while(cin>>numberOfSeat)
10     {
11         cin>>seats;
12         for(int i = 0; i < numberOfSeat; i++)
13         {
14             if(seats[i] == '*')    // if the current char is '*', we just move one step
15                 continue;
16             else                   // if the current char is '@', we move two steps, no matter what the next char is
17             {
18                 i++;
19                 numberOfBook++;
20             }
21         }
22         if(numberOfBook == 0)
23             cout<<"Oh no!"<<endl;
24         else
25             cout<<numberOfBook<<endl;
26         numberOfBook = 0;
27     }
28 }
收获园豆:5
Mushroom0417 | 菜鸟二级 |园豆:327 | 2014-03-01 18:13

少了一本书能占相邻2个座位的逻辑

吴瑞祥 | 园豆:29449 (高人七级) | 2014-03-01 21:14

@吴瑞祥: 是不是 最后少了个return 0;

张慧慧 | 园豆:149 (初学一级) | 2014-03-02 13:25

大神,谢谢了,不过我电脑编译器不能用,你运行时对么?

张慧慧 | 园豆:149 (初学一级) | 2014-03-02 13:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册