qq_39347661 2019-05-07 11:32 采纳率: 33.3%
浏览 297
已采纳

uva230总是wronganwser,不知道为什么?

我在刷uva230遇到的问题,代码在本地测试都没问题,但是一提交就是WrongAnswer,不知道问题具体出在哪,求大神指点迷津。不要网上的代码,希望大神能帮忙找出我代码的错误。

#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<string>
#include<iterator>

using namespace std;
struct bookList {
    string bookName;
    string author;
};
bool cmp(bookList l, bookList r) {
    if (l.author == r.author)return l.bookName < r.bookName;
    else return l.author < r.author;
}

string bookName;
string author;
string temp;
bookList L1[10000];
map<string, int> bookNum;
vector<int> bookShelves;
int n = 0;
void T5_8() {
    while (cin >> temp) {
        bookName.clear();
        author.clear();
        if (temp == "END")break;
        bookName += temp;
        while (cin >> temp&&temp != "by") {
            bookName += " ";
            bookName += temp;
        }
        getline(cin, author);
        L1[n].bookName = bookName;
        L1[n].author = author;
        n++;
    }

    //排序
    sort(L1, L1 + n, cmp);

    //赋予编号,并把图书上架
    for (int i = 0; i < n; i++) {
        bookNum[L1[i].bookName] = i;
        bookShelves.push_back(i);
    }

    vector<int> books;
    //处理输入;
    while (cin >> temp) {
        if (temp[0] == 'B') {
            getchar();//吃掉空格
            getline(cin, bookName);
            vector<int>::iterator it = find(bookShelves.begin(), bookShelves.end(), bookNum[bookName]);
            bookShelves.erase(it);

        }
        else if (temp[0] == 'R') {
            getchar();//吃掉空格
            getline(cin, bookName);
            books.push_back(bookNum[bookName]);
        }
        else if (temp[0] == 'S') {
            sort(books.begin(), books.end());//先排序
            while (!books.empty()) {
                int bookNumber = books.front();
                vector<int>::iterator it = books.begin();
                books.erase(it);
                if (bookShelves.empty()) {
                    bookShelves.push_back(bookNumber);
                    cout << "Put ";
                    cout << L1[bookNumber].bookName;
                    cout << " first\n";
                }
                else {
                    int toFind = bookNumber;//把这个插入到num比他小的最大的数的后面
                    int insertMax = -1;
                    for (int i = 0; i < bookShelves.size(); i++) {
                        if (bookShelves[i] < toFind) {
                            if (insertMax<bookShelves[i])
                                insertMax = bookShelves[i];
                        }
                        else break;
                    }

                    if (insertMax == -1) {//放在最前面
                        cout << "Put ";
                        cout << L1[bookNumber].bookName;
                        cout << " first\n";
                    }
                    else {
                        cout << "Put ";
                        cout << L1[toFind].bookName;
                        cout << " after ";
                        cout << L1[insertMax].bookName;
                        cout << "\n";
                        //cout << "Put " << L1[toFind].bookName << " after " << L1[insertWho].bookName << endl;
                    }
                    bookShelves.insert(find(bookShelves.begin(), bookShelves.end(), insertMax), toFind);
                }
            }
            cout << "END" << endl;
        }
        else { break; }
    }
}

int main() {
    T5_8();
}



  • 写回答

2条回答 默认 最新

  • CaptainXue 2019-05-09 09:49
    关注

    问题所在:

    # 1、对于这种含有空格的字符串输入,用getline读入,因为输入数据中,并没有明确单词与单词之间只有一个空格,你这种处理输入的方式不对****

    # 2、bookList L1[10000]; 这个需要改成 vector L1; 你这里调用了vector的相关操作

    3、你最后在更新书架上的书的时候,应该分开处理,当insertMax为-1时,应该单独写一个在开始位置插入该书的编号,因为你在vector中无法找到位置为-1的。

    # 3、修改后的程序代码如下: (以后有什么问题可以直接问我哦!)**_**

    #include

    #include

    #include

    #include

    #include

    #include

    #include

    using namespace std;

    struct bookList {

    string bookName;

    string author;

    };

    bool cmp(bookList &l, bookList &r) {

    if (l.author == r.author)

    return l.bookName < r.bookName;

    else

    return l.author < r.author;

    }

    string bookName;

    string author;

    string temp;

    //bookList L1[10000];

    vector L1;

    map bookNum;//书名和编号对应

    vector bookShelves;

    int n = 0;

    void T5_8() {

    while(getline(cin,temp)&&(temp!="END")) { //读取每一行

    int pos = temp.rfind('"'); //从字符串的尾部往前找到第一个"

    bookList l1;

    l1.bookName = temp.substr(0, pos + 1);

    l1.author = temp.substr(pos + 5);

    // cout << n << ":[" << l1.bookName << "],[" << l1.author << "]" << endl;

    n++;

    L1.push_back(l1);

    }

    //排序

    sort(L1.begin(), L1.end(), cmp);

    //赋予编号,并把图书上架

    for (int i = 0; i < n; i++) {

    // cout << i << ":[" << L1[i].bookName << "],[" << L1[i].author << "]" << endl;

    bookNum[L1[i].bookName] = i;

    bookShelves.push_back(i);

    }

    vector books;

    //处理输入;

    while (cin >> temp) {

    if (temp[0] == 'B') {

    getchar();

    getline(cin, bookName);

    bookShelves.erase(find(bookShelves.begin(), bookShelves.end(), bookNum[bookName]));

    } else if (temp[0] == 'R') {

    getchar();//吃掉空格

    getline(cin, bookName);

    books.push_back(bookNum[bookName]);

    } else if (temp[0] == 'S') {

    sort(books.begin(), books.end());//先排序

    while (!books.empty()) {

    int bookNumber = books.front();

    vector::iterator it = books.begin();

    books.erase(it);

    int toFind = bookNumber;//把这个插入到num比他小的最大的数的后面

    int insertMax = -1;

    for (int i = 0; i < bookShelves.size(); i++) {

    if (bookShelves[i] < toFind) {

    if (insertMax<bookShelves[i])

    insertMax = bookShelves[i];

    } else

    break;

    }

    if (insertMax == -1) { //放在最前面

    cout << "Put " << L1[bookNumber].bookName << " first" << endl;

    bookShelves.insert(bookShelves.begin(),toFind);

    } else {

    cout << "Put " << L1[toFind].bookName << " after " << L1[insertMax].bookName << endl;

    //当是这里,如果是第一本书,本应该插入在第一个位置,这里会查找不到,插入在最后一个位置

    bookShelves.insert(find(bookShelves.begin(), bookShelves.end(), insertMax) + 1, toFind);

    }

    }

    cout << "END" << endl;

    } else {

    break;

    }

    }

    }

    int main() {

    T5_8();

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器