1 条题解

  • 0
    @ 2025-4-27 18:52:06

    Pascal :

    var
            ycl:array[1..8] of longint=(10,100,1000,10000,100000,1000000,10000000,100000000);
            a:array[0..1000] of longint;
            b:array[0..1000,1..2] of longint;
            n,q,i,j:longint;
    procedure qsort(l,r:longint);
    var
            i,j,mid,t:longint;
    begin
            i:=l;
            j:=r;
            mid:=a[(l+r) div 2];
            repeat
                    while a[i]<mid do inc(i);
                    while a[j]>mid do dec(j);
                    if i<=j then
                    begin
                            t:=a[i];
                            a[i]:=a[j];
                            a[j]:=t;
                            inc(i);
                            dec(j);
                    end;
            until i>j;
            if i<r then qsort(i,r);
            if l<j then qsort(l,j);
    end;
    begin
            readln(n,q);
            for i:=1 to n do
                    read(a[i]);
            for i:=1 to q do
                    read(b[i,1],b[i,2]);
            qsort(1,n);
            for i:=1 to q do
            begin
                    j:=1;
                    while (a[j] mod ycl[b[i,1]]<>b[i,2]) and (j<=n) do inc(j);
                    if j>n then writeln(-1)
                    else writeln(a[j]);
            end;
    end.
    
    

    Java :

    
    import java.util.Scanner;
    
    public class Main {
    	
    	static char a[][] = new char[1001][1001];
    	
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    
    		int book[] = new int[1001];
    		int len[] = new int[1001];
    		int num[] = new int[1001];
    
    		int n = sc.nextInt();
    		int q = sc.nextInt();
    				
    		for(int i=1;i<=n;i++){
    			book[i] = sc.nextInt();
    		}
    		
    	    for(int i=1;i<=q;i++) {
    	        len[i] = sc.nextInt();
    	        num[i] = sc.nextInt();
    	        // 10的 len[i]次方
    	        int tmp = (int) Math.pow(10,len[i]),min=Integer.MAX_VALUE;
    	        for(int j=1;j<=n;j++) {
    	        	if(book[j]%tmp==num[i]&&book[j]<min){
    	        		min=book[j];
    	        	}   	
    	        }
    	        if(min!=Integer.MAX_VALUE){
    	        	System.out.println(min);
    	        }else{
    	        	System.out.println(-1);
    	        }
    	    }    
    		   
    		sc.close();
    	}
    }
    
    
    • 1

    信息

    ID
    1162
    时间
    1000ms
    内存
    16MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者