• 洛谷 P1571 眼红的Medusa


     1 /*第一种,就是写二分函数
     2 因为要按照a数组的顺序输出,那么则把b数组排序之后进行二分查找
     3 若a中该元素存在b数组,则输出即可
     4 */
     5 #include<stdio.h>
     6 #include<stdlib.h>
     7 #include<string.h>
     8 
     9 int binary_search(int* a, int x, int left, int right);//二分查找
    10 int cmp(const void* a, const void* b);//快排的cmp函数
    11 
    12 int a[100001] = { 0 }, b[100001] = { 0 };
    13 
    14 int main(void) {
    15     int n, m;
    16 
    17     scanf("%d %d", &n, &m);
    18     for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    19     for (int i = 0; i < m; i++)    scanf("%d", &b[i]);
    20     qsort(b, m, sizeof(int), cmp);
    21 
    22     int flag = 0;//空格处理
    23     for (int i = 0; i < n; i++) {
    24         if (binary_search(b, a[i], 0, m - 1)) {
    25             if (!flag) { printf("%d", a[i]); flag = 1; }
    26             else printf(" %d", a[i]);
    27         }    
    28     }
    29 
    30     return 0;
    31 }
    32 
    33 int cmp(const void* a, const void* b) {
    34     return *(int*)a - *(int*)b;
    35 }
    36 
    37 int binary_search(int* a, int x, int left, int right) {
    38     int mid;
    39     while (left <= right) {
    40         mid = (right + left) >> 1;// 除二
    41         if (a[mid] == x) return 1;
    42         else if (a[mid] > x)    right = mid - 1;
    43         else left = mid + 1;
    44     }
    45     return 0;
    46 }
     1 /*第二中,用C++ STL中的Binary search,存在返回真,否则假
    2 因为要按照a数组的顺序输出,那么则把b数组排序之后进行二分查找 3 若a中该元素存在b数组,则输出即可 4 */ 5 #include<stdio.h> 6 #include<stdlib.h> 7 #include<string.h> 8 #include<algorithm> 9 using namespace std; 10 11 int cmp(const void* a, const void* b);//快排的cmp函数 12 13 int a[100001] = { 0 }, b[100001] = { 0 }; 14 15 int main(void) { 16 int n, m; 17 18 scanf("%d %d", &n, &m); 19 for (int i = 0; i < n; i++) scanf("%d", &a[i]); 20 for (int i = 0; i < m; i++) scanf("%d", &b[i]); 21 qsort(b, m, sizeof(int), cmp); 22 23 int flag = 0;//空格处理 24 for (int i = 0; i < n; i++) { 25 if (binary_search(b, b + m, a[i])) { 26 if (!flag) { printf("%d", a[i]); flag = 1; } 27 else printf(" %d", a[i]); 28 } 29 } 30 31 return 0; 32 } 33 34 int cmp(const void* a, const void* b) { 35 return *(int*)a - *(int*)b; 36 }
  • 相关阅读:
    源码分析shiro认证授权流程
    shiro功能模块
    Java NIO(New I/O)的三个属性position、limit、capacity
    阻塞和非阻塞,同步和异步 总结
    小白科普:Netty有什么用?
    【异常】SSM整合quartz之org.quartz.impl.JobDetailImpl----ClassNotFoundException
    笔记本键盘字母按出来的都是快捷键
    eclipse egit提交方法
    oracle批量insert
    CAS Client 3.2.1 配置详解
  • 原文地址:https://www.cnblogs.com/letianpaiai/p/12836011.html
Copyright © 2020-2023  润新知