30分求助

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

zzyxl_qaq @ 2022-12-21 11:06:36

#include<bits/stdc++.h>
using namespace std;
int s[1005];
bool fl[1005];
bool check(int a,int b){
    int i=1;
    while(i<=a&&i<=b){
        if(a%i!=b%i)return false;
        i*=10;
    }
    return true;
}
int main(){
    int n,q,w,num;
    cin>>n>>q;
    for(int i=1;i<=n;i++)cin>>s[i];
    sort(s+1,s+n+1);
    for(int i=1;i<=q;i++){
        cin>>w>>num;
        bool flag=true;
        for(int j=1;j<=n;j++){
            if((s[j]>=num)&&check(s[j],num)&&fl[j]==false){
                cout<<s[j]<<endl;
                fl[j]=true;
                flag=0;
                break;
            }
        }
        if(flag)cout<<-1<<endl;
    }
    return 0;
}

by mooktian @ 2023-03-07 19:27:42

#include <bits/stdc++.h>
#define f(i,a,b) for(int i=a;i<=b;i++)
#define g(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
int n,q,book[1001],len,code,flag; 
int main(){
    cin>>n>>q;
    f(i,1,n) cin>>book[i];
    sort(book+1,book+n+1);
    f(i,1,q) {
        cin>>len>>code;
        f(j,1,n) {
            if(book[j]%((int)pow(10,len))==code) {
                printf("%d\n",book[j]);
                flag=1;
                break;
            }
        }
        if(!flag) printf("%d\n",-1);
        flag=0;
    }
    return 0;

}

|