zoukankan      html  css  js  c++  java
  • can you find it?

    #include <stdio.h>

    #include <algorithm>

    using namespace std;
    int ab[1000020],a[1024],b[1024],c[1024];
    int find(int x,int l,int r)
    {
    int mid;
    if(r<l) return 0;
    mid=(l+r)/2;
    if(ab[mid]==x) return 1;
    else if(ab[mid]>x) return find(x,l,mid-1);
    else return find(x,mid+1,r);
    }
    int main()
    {
             int x,y,z,i,j,n,m,t,k,q=0;
       while(scanf("%d %d %d",&x,&y,&z)!=EOF)
            {   
                       t=0;q++;
                       for(i=0;i<x;i++)
                           {
                               scanf("%d",&a[i]);
                           }
                       for(i=0;i<y;i++)
                            {
                               scanf("%d",&b[i]);
                             }
                       for(i=0;i<z;i++)
                            {
                               scanf("%d",&c[i]);
                            }
                       for(i=0;i<x;i++)
                       {
                             for(j=0;j<y;j++)
                                 {
                                       ab[t++]=a[i]+b[j];
                                 }
                      }
                  sort(ab,ab+t+1);
                   scanf("%d",&n);
              printf("Case %d: ",q);
              while(n--)
             {
                 k=0;
                scanf("%d",&m);
               for(j=0;j<z;j++)
              {
                         if(find(m-c[j],0,t))
                          {
                                  k=1;
                                 continue;
                            }
              }
                 if(k==1) printf("YES ");
                   else printf("NO ");
           }
    }
    return 0;
    }

    运用了二分查找的方法;

    查找前一定要排序;

    函数 sort(ab,ab+t+1);  前面为数组名,后面为数组名加数组长度;

    头文件

    #include <algorithm>

    using namespace std;

  • 相关阅读:
    一些python练习题
    爬取某房源网数据
    练习1:定义 is_Even 函数,传一 int 参数,判断是否是偶数,return True;不是,return False
    pycharm中tkinter 不显示窗口
    保护眼睛颜色的RGB数值
    Python study----------class的实例化
    HTML----------STUDY
    Python study----------字典 popitem()
    Python study----------Python搜索路径
    Python study----------第三方module-----chardet(识别编码)
  • 原文地址:https://www.cnblogs.com/liuming1115/p/4479704.html
Copyright © 2011-2022 走看看