Ducky Debugging(字符串输入)题解

导读:本篇文章讲解 Ducky Debugging(字符串输入)题解,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

题目描述:

You don’t know anything about programming, but nonetheless you and your friend Bob joined the BAPC. Bob just submitted a solution to a problem, but sadly his submission was not accepted. You will help him fifigure out what mistake he made. In order to do so, he will explain what every part of the program should do according to him. He does not want you to interrupt him while he is explaining, but whenever he is unsure of

something, he will ask for confifirmation. Bob is actually a very good coder, so whenever he asks for confifirmation, he is simply right. You just need to react by nodding and saying “Quack!”. As long as you don’t interrupt him at the wrong time, and as long as you say “Quack!” at the right times, he will eventually fifind his mistake and he will shout “I quacked the code!”.

输入描述:

This is an interactive problem. Your submission will be run against an interactor, which reads the standard output of your submission and writes to the standard input of your submission. This interaction needs to follow a specifific protocol:

The interactor sends one line of text at a time, each line being one sentence of Bob’s monologue. You need to reply as follows:

    • If the line ends with a question mark (“?”), your reply must be the string “Quack!”.

    • If the line ends with a period (“.”), your reply must be the string “*Nod*”.

    • If the line is exactly “I quacked the code!”, your submission should exit. Reading more input will result in a time limit exceeded and printing more output will result in a wrong answer.

Bob’s monologue will be no more than 100 sentences, with at most 100 characters in total. It is guaranteed that every sentence, except the last one, ends with a question mark (“?”) or a period (“.”).

Make sure you flflush the buffer after each write.

A testing tool is provided to help you develop your solution.

输出描述:

示例1

输入

In the loop, I read a line of input.

The method std::getline(std::cin, line); reads until a newline, right?

Then I branch according to the last character of the line of input.

When the character is an exclamation mark, I terminate the program.

When it is a question mark, I need to print “Quack!” right?

In any other case, I simply print “*Nod*”.

Wait, to flush the output stream I need to use std::endl instead of ‘\n’ right?

I quacked the code!

输出

*Nod*

Quack!

*Nod*

*Nod*

Quack!

*Nod*

Quack!

示例2

输入

My program seems to always exit after reading one line.

Shouldn’t it only halt when a line ends with an exclamation mark?

After reading a line, I check the last character using a switch.

When the character is a period, I print “*Nod*”.

When the character is a question mark, I print “Quack!”.

Wait, both get printed before the program exits?

I guess I forgot to add break; here and there…

I quacked the code!

输出

*Nod*

Quack!

*Nod*

*Nod*

*Nod*

Quack!

*Nod*

思路

题意为输入多行话。

每行话结尾以’.’结尾,就输出”*Nod*”;结尾以’?’结尾,就输出”Quack!”;

当输入的是I quacked the code!时,不用回答,停止输入。

重点

不能用cin>>s进行输入。因为cin遇到空格会结束输入。

应该用getline(cin,s)读取整行(以回车结束)。

AC代码

#include<iostream>
#include<string>
using namespace std;
string s;
int main(){
    ios_base::sync_with_stdio(false);
    while(1){
        getline(cin,s);
        fflush(stdout);
        if(s[s.size()-1]=='!'){
            break;
        }
        else{
            if(s[s.size()-1]=='.'){
                cout<<"*Nod*"<<endl;
            }
            else if(s[s.size()-1]=='?'){
                cout<<"Quack!"<<endl;
            }
        }
        
    }
}

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/103323.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!