0

この問題の C++ コードを書きました。問題は基本的に、オープニング"をに置き換え``、クロージング"をに置き換えること''です。

それは非常に簡単ですが、私は間違った答えを得ています。誰かが私のコードの問題を見つけるのを手伝ってくれますか?

サンプル入力:

"To be or not to be," quoth the Bard, "that
is the question".
The programming contestant replied: "I must disagree.
To `C' or not to `C', that is The Question!"

出力例:

``To be or not to be,'' quoth the Bard, ``that
is the question''.
The programming contestant replied: ``I must disagree.
To `C' or not to `C', that is The Question!''

コード:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>

using namespace std;

int main(){
    string inputString;

    while (getline(cin, inputString)){
        int nDoubleQuotes = 0;

        for (int i = 0 ; i < (int)inputString.length(); ++i){
            if (inputString[i] == '"'){
                ++nDoubleQuotes;

                nDoubleQuotes = nDoubleQuotes%2;

                if (nDoubleQuotes == 1)
                    cout << "``";
                else
                    cout << "''";
            }
            else
                cout << inputString[i];
        }

        cout << '\n';
        inputString.clear();
    }
    return 0;
}
4

2 に答える 2

2

残念ながら、あなたのコードはサンプル テストケースにも合格しません! とにかく、この行をループint nDoubleQuotes = 0;の外に置くだけwhile( getline( cin , inputString ) )です。それを行う必要がある理由は、入力ファイルでは、サンプル テスト ケースのように、引用符 (") を 1 行で開始し、次の任意の行で終了できます。問題文に示されています:

The programming contestant replied: "I must disagree. #quote start on this line
To `C' or not to `C', that is The Question!" #quote ends on this

すべての行で引用カウンター変数を初期化すると、引用マーカーの開始と終了が同じ行であると想定されますが、これは間違っています。

于 2013-07-24T05:43:52.887 に答える