• (TOJ3114){A}∩{B}


    描述

    Given two integer sets A and B sorted descendingly, you are asked to output the element count of A∩B.

    输入

    Standard input will contain multiple test cases. The first line of the input is a single integrate T (1 <= T <= 50) which is the number of test cases. then T consecutive test cases followed. In each test case, there has four lines. The first line contains the element count N(1<=N<=100000) for the first set. The second line contains N integers in the first set.
    The third line contains the element count M(1<=M<=100000) for the second set. The fourth line contains M integers in the second set.

    NOTE: there will be no duplicate elements in each sets and all the integers in each set have been sorted descendingly.

    输出

    For each test case in the input, there's only one line output that contains the element count of the intesection of the two sets.

    样例输入

    1
    5
    5 4 3 2 1
    4
    5 3 1 -1

    样例输出

    3
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<ctype.h>
     4 #include<math.h>
     5 
     6 int a[100001],b[100001];
     7 
     8 void deal(int c[], int n, int d[], int m)
     9 {
    10     int i,j,flag;
    11     flag=i=j=0;
    12     while(i<n && j<m){
    13         if(c[i]<d[j]){j++;}
    14         else if(c[i]==d[j]){
    15             flag++;
    16             i++; j++;
    17         }else{
    18           i++;    
    19         }
    20     }
    21     printf("%d\n",flag);
    22 }
    23 
    24 void solve()
    25 {
    26     int T,n,m,i;
    27     scanf("%d",&T);
    28     while(T--){
    29         scanf("%d",&n);
    30         for(i=0; i<n; i++){
    31             scanf("%d",&a[i]);
    32         }
    33         scanf("%d",&m);
    34         for(i=0; i<m; i++){
    35             scanf("%d",&b[i]);
    36         }
    37         deal(a,n,b,m);        
    38     }
    39 }
    40 
    41 int main()
    42 {
    43     solve();
    44     return 0;
    45 }
     
  • 相关阅读:
    Java入门第37课——猜字母游戏之设计数据结构
    Sublime Text 3 常用快捷键
    WEB前端响应式布局之BootStarp使用
    js让页面逐渐变透明,直到消失
    Vue实战之插件 sweetalert 的使用
    搭建jQuery开发环境
    Layui数据表单的编辑
    SpringBoot基于websocket的网页聊天
    layui修改数据的时候下拉框和选择框默认选中
    Linux 软件编译、安装、删除
  • 原文地址:https://www.cnblogs.com/xueda120/p/3095045.html
Copyright © 2020-2023  润新知