Los solution to a problem Valley P3955 [librarian]

# P3955 solution to a problem [librarian](On how lazy librarian)

Luo Gu original: https://www.luogu.com.cn/blog/jvruo-0076/solution-p3955

Some ideas

首先,读入并存入数组,排列。取余查找*,有就输出**,无输出-1
* 用循坏!千万别用pow函数。虽然洛谷上pow能过,但一个系统一个数据。别的网站由于实数整数原因,过不了!!!
**因为先前已经排列,所以没必要再去找。一来浪费时间,二来不会输出最小的

$ \ Color {white} \ colorbox {white} {\ text {Title III solution to a problem, management greatly begged! ! ! }} $

Code

#include<bits/stdc++.h>
using namespace std;
int n,q,ans,noww;
int a[1005],b[1005],c[1005];
int main()
{
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);//读并存入
    sort(a+1,a+n+1);//排它!!!
    for(int i=1;i<=q;i++)
        scanf("%d%d",&b[i],&c[i]);//读入
    for(int i=1;i<=q;i++)
    {
         ans=-1;noww=1;//重新定义,避免之前的影响这一次运算
         //while(b[i]--)noww*=10;
         while(c[i]>=noww)noww*=10;/*两种循坏方式*/
         for(int j=1;j<=n;j++)    
           if(a[j]%noww==c[i])//取余判断
           {
            ans=a[j];           
            break;
           }  
           printf("%d\n",ans);//输出
    }
    return 0;
}

Finally, after a

Guess you like

Origin www.cnblogs.com/107003CN171yunbingche/p/12040496.html