pat 2-12. 两个有序链表序列的交集

来源:互联网 发布:阿里云域名未备案 编辑:程序博客网 时间:2024/05/16 23:02

代码:

#include<cstdio>#include<cstring>#include<iostream>using namespace std;int a[10000000],b[10000000];int main(){    int len1=0,len2=0;    while(1)    {        int x;        scanf("%d",&x);        if(x==-1)            break;        else            a[len1++]=x;    }    while(1)    {        int x;        scanf("%d",&x);        if(x==-1)            break;        else            b[len2++]=x;    }    int i=0;    int j=0;    int cnt=0;    while(i<len1&&j<len2)    {        if(a[i]==b[j])        {            if(cnt==0)                printf("%d",a[i]);            else                printf(" %d",a[i]);            cnt++;            i++;            j++;        }        else if(a[i]<b[j])        {            i++;        }        else            j++;    }    if(cnt==0)        printf("NULL\n");    else        printf("\n");    return 0;}


0 0
原创粉丝点击