c++ - 演示文稿错误 :Word Reversal

标签 c++

我对这个问题有疑问 我不知道我的代码有什么问题,每次我都会收到 Presentation Error 我不知道输出的格式是什么 你能帮我解决这个问题吗 很抱歉我的代码有点困惑

这里是问题的链接 http://sharecode.ir/section/problemset/problem/1208

#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
int main()
{
    string temp=" ";
    bool cheak3=false,cheak4=false;
    int n,num;
    cin>>n;
    while(n != 0)
    {
        if(cheak4 == true)
            cout<<endl;
        cheak4=true;
        cin>>num;
        cheak3=false;
        string cheak1,cheak;
        while(1)
        {
            if(num ==-1)
                break;
            getline(cin,temp);
            for(int i=0 ; i<temp.size() ; i++)
            {
                if(temp[i] != ' ')
                    cheak.push_back(temp[i]);
                else
                {
                    reverse(cheak.begin(),cheak.end());
                    cheak1+=cheak;
                    cheak.clear();
                    if(cheak3 == true)
                        cheak1.push_back(' ');
                }
            }
            reverse(cheak.begin(),cheak.end());
            cheak1+=cheak;
            cheak.clear();
            num--;
            if(cheak3 == true)
            {
                cheak1.push_back(' ');
                cout<<cheak1<<endl;
                cheak1.clear();
            }
            cheak3=true;
            }
        n--;
    }
}

最佳答案

我认为棘手的部分是您应该在输出 block 之间打印一个空行。

你的代码逻辑太复杂了!这是我对这个问题的解决方案:

#include <iostream>
#include <string>
using namespace std;    
int main() {
    int N, lines;
    string word;
    cin>>N;
    while (N--) {
        cin>>lines;
        while (lines--) {
            char end;
            do {
                cin >> word;
                end = cin.get();
                for (int i=word.length()-1;i>=0;i--) cout<<word[i];
                cout << end;
            } while (end != '\n');
        }
        if (N) cout << endl;
    }
    return 0;
}

if (N) cout << endl;确保为每个输出 block 打印一个换行符,除了最后一个(当 N 等于 0 时)。

读完一行中的每个单词后,可以使用cin.get();以确定下一个字符。如果是空格,则打印它并阅读下一个单词。否则如果它是 \n打印它并转到下一行! :)

关于c++ - 演示文稿错误 :Word Reversal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20500493/

相关文章:

c++ - char * 段错误

c++ - C++ 中有没有一种方法可以在没有构造函数的情况下预初始化结构或类?

C++ - 输出给定长度的所有可能的 DNA kmers

java - 如何标准化混合的 WAV 文件

c++ - 如何在 C++ 中遍历 google protobuf 映射?

c++ - 使用 exit(1) 时释放内存,c++

c++ - 如何开始用 C++ 编写音乐可视化工具?

c++ - 使用 mpi 将矩阵写入单个 txt 文件

c++ - vector 比较

c++ - 帮助 boost::regex 修剪