UVA10474 两种代码同样的输出,为什么我这个是RE呢?
查看原帖
UVA10474 两种代码同样的输出,为什么我这个是RE呢?
376953
冬日暖陽楼主2020/8/26 19:50

同AC的代码同样是一样的输出,这个代码为什么是RE呢?题目是Where is the Marble UVA10474 -----------------代码如下---------------------

/**先输入n和m两个数字,n为大理石的个数,每块大理石上都有一个数字,
在下面依次输入这n个数;m代表你接下来要在这些大理石上寻找m个数字,
在最后依次输入你要找的这m个数,如果找到了就要输出那个数在大理石上排第几(按从小到大),如果没有找到就输出not found.

可以有多个测试用例。测试用例总数小于65个。
每个测试用例都由2个整数组成:N是弹珠的数量,
Q是Mina将进行的查询的数量。接下来的N行将包含写在N个弹珠上的数字。
这些大理石数字不会以任何特定的顺序出现。下面的Q行将有Q查询。
请确保,输入的数字都不大于10000,而且都不是负数。输入被N = 0和Q = 0的测试用例终止。*/
#include <iostream>
#include <utility>
#include <vector>
#include <cmath>

using std::cin;
using std::cout;
using std::endl;
using std::ios;

void bubbleSort(int arr[], int N) {
    bool flag = true;
    for (int i = 0; i < N && flag == true; i++) {        //冒泡排序
        flag = false;
        for (int j = 0; j < N - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                std::swap(arr[j], arr[j + 1]);
                flag = true;
            }
        }
    }
}

void Search(int arr[], int N, int que[][2], int Q) {
    for (int i = 0; i < Q; i++) {
        for (int j = 0; j < N; j++) {
            if (que[i][0] == arr[j]) {
                que[i][1] = j + 1;
                break;
            }
            else {
                que[i][1] = -1;
            }
        }
        if(N == 0)  //补充代码。N=0的情况
            que[i][1] = -1;
    }
}

/*void print(int arr[], int N) {
    for(int i = 0; i < N; i++) {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}*/

void printQue(int que[][2], int Q) {
    ///cout << "CASE# " << count << ":" << endl;    这是个示例的坑,在此输出不可以放这,而是在输入要查找的数之前输入。
    for (int i = 0; i < Q; i++) {
        if (que[i][1] != -1) {
            cout << que[i][0] << " found at " << que[i][1] << endl;
        }
        else {
            cout << que[i][0] << " not found" << endl;
        }
/*        if (i < Q - 1) {     ///最后一行不能有回车!(Apricity注:可能有此项)
            cout << endl;
        }*/

    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ///朴实做法

    ///有可能为无限输入,因此需要循环和 键入0 0 的终止条件

    ///1.输入部分
    int N, Q, count = 0;
    int arr[65];
    int que[65][2];
    while (cin >> N >> Q) {
        if (N == 0 && Q == 0)
            break;
        count++;
        for (int i = 0; i < N; i++) {
            cin >> arr[i];
        }
        if(N == 0)  //补充代码。N=0的情况
            arr[0] = -1;

        cout << "CASE# " << count << ":" << endl;   ///此语句应该放这里,在输入查找的数字之前输出CASE xxx
        for (int i = 0; i < Q; i++) {
            cin >> que[i][0];
        }
        ///2.解题部分 排序
        bubbleSort(arr, N);
        Search(arr, N, que, Q);
        ///3.输出结果
        //print(arr, N);  //测试
        printQue(que, Q);   //测试
        //要输出 CASE# 1: 这类字段
    }

    return 0;
}

--------下面是某网上给的通过的代码------------ 作者为:CSDN 海岛Blog 2016-08-02 22:40:07

仅做对比用,无侵权之意。有稍加的输入输出流的修改,不过还是Accept。

#include <bits/stdc++.h>

using namespace std;

#define MAXN 11000

int marble[MAXN];

void print(int val,int no) {
            if(marble[no] == val)
                cout << val << " found at " << no+1 << endl;
                //printf("%d found at %d\n", val, no + 1);
            else
                //printf("%d not found\n", val);
                cout << val << " not found" << endl;
}


int main()
{
    int n, q, caseno=0, val;

    while(cin>>n>>q/*scanf("%d%d", &n, &q) != EOF*/) {
        if(n == 0 && q == 0)
            break;

        for(int i=0; i<n; i++)
            //scanf("%d", &marble[i]);
            cin>>marble[i];

        sort(marble, marble + n);

        //printf("CASE# %d:\n", ++caseno);
        cout << "CASE# " << ++caseno << ":" << endl;
        while(q--) {
            //scanf("%d", &val);
            cin>>val;
            int no = lower_bound(marble, marble + n, val) - marble;
                        print(val,no);
        }
    }

    return 0;
}

2020/8/26 19:50
加载中...