P3955求助

P3955 [NOIP2017 普及组] 图书管理员

jimmyshi29 @ 2021-10-03 19:59:23

# include <iostream>
# include <algorithm>
# include <cstring>
using namespace std;

int a[1005], f[1005];

struct Need
{
    int l, x;
};

Need s[1005];

bool check(int x, int y)
{
    int q = 0;
    while (y)
    {
        q = q * 10 + y % 10;
        y /= 10;
    }
    int p = 0;
    while (x)
    {
        p = p * 10 + x % 10;
        x /= 10;
        if (p == q)
            return true;
    }
    return false;
}

int main()
{
//  freopen("librarian.in", "r", stdin);
//  freopen("librarian.out", "w", stdout);
    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    memset(f, true, sizeof(f));
    for (int i = 1; i <= q; i++)
        cin >> s[i].l >> s[i].x;
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= q; i++)
    {
        bool flag = false;
        for (int j = 1; j <= n; j++)
        {
            if (check(a[j], s[i].x) && f[j])
            {
                flag = true;
                f[j] = false;
                cout << a[j] << endl;
                break;
            }
        }
        if (!flag)
            cout << -1 << endl;
    }
    return 0;
}

哪位大佬能看看,这题哪错了,本地评测错误数据是对的啊


by _l_l_ @ 2021-10-03 20:00:27

你的代码可能有 \texttt{UB}


by Lydia_qwq @ 2021-10-03 20:01:02

提供代码

#include<bits/stdc++.h>
using namespace std;
int book[1001],num[1001];
int main()
{
    int n,m,a,b,flag,z;
    unsigned long long x;
    cin>>n>>m;
    for(int i=0;i<n;i++)cin>>book[i];
    for(int i=0;i<m;i++)
    {
        z=0;
        flag=0;
        cin>>a>>b;
        x=pow(10,a);
        for(int j=0;j<n;j++)
        {
            if(book[j]%x==b)
            {
                flag=1;
                num[z]=book[j];
                z++;
            }
        }
        if(flag==0)cout<<-1<<'\n';
        else
        {
            sort(num,num+z-1);
            cout<<num[0]<<'\n';
        }
    }
    return 0;
}

只是见解一下就行了,我70分你复制也没用(doge)


by jimmyshi29 @ 2021-10-03 20:01:37

@disangan223 ???


by _l_l_ @ 2021-10-03 20:03:51

@jimmyshi29 https://blog.csdn.net/houzijushi/article/details/102754339 这就是 UB


|