zoukankan      html  css  js  c++  java
  • 7-52 两个有序链表序列的交集 (20分)

    最后一个测试点超时

     1 #include <iostream>
     2 #include <vector>
     3 #include <string>
     4 using namespace std;
     5 int main()
     6 {
     7     vector<int>l1, l2, l3;
     8     while (1)
     9     {
    10         int in;
    11     scanf("%d",&in);
    12         if (in == -1)
    13             break;
    14         l1.push_back(in);
    15     }
    16     while (1)
    17     {
    18         int in;
    19         scanf("%d",&in);
    20         if (in == -1)
    21             break;
    22     l2.push_back(in);
    23     for (int i = 0; i < l1.size(); i++)
    24     {
    25       if(in == l1[i])
    26       {
    27         l3.push_back(l1[i]);
    28                 if (l3.size() == 1)printf("%d",l1[i]);
    29                 else printf(" %d",l1[i]);
    30             }
    31     }
    32     }
    33     if (l1.empty() || l2.empty() || l3.empty())
    34     {
    35         cout << "NULL";
    36     }
    37 }
     1 #include <iostream>
     2 using namespace std;
     3 typedef struct node
     4 {
     5     int id;
     6     struct node* next;
     7 }*L;
     8 int main()
     9 {
    10     L l1 = NULL, l2 = NULL, l3 = NULL;
    11     int cnt = 0;
    12     int in;
    13     L p = NULL;
    14     while (1)
    15     {
    16         scanf("%d", &in);
    17         if (in == -1)break;
    18         cnt++;
    19         if (NULL == l1)
    20         {
    21             l1 = new struct node;
    22             l1->id = in;
    23             l1->next = NULL;
    24             p = l1;
    25         }
    26         else
    27         {
    28             struct node* s = new struct node;
    29             s->next = NULL;
    30             s->id = in;
    31             l1->next = s;
    32             l1 = l1->next;
    33         }
    34     }
    35     l1 = p;
    36     while (1)
    37     {
    38         cin >> in;
    39         if (in == -1)break;
    40         cnt++;
    41         if (NULL == l2)
    42         {
    43             l2 = new struct node;
    44             l2->id = in;
    45             l2->next = NULL;
    46             p = l2;
    47         }
    48         else
    49         {
    50             struct node* s = new struct node;
    51             s->next = NULL;
    52             s->id = in;
    53             l2->next = s;
    54             l2 = l2->next;
    55         }
    56     }
    57     int flag = 0;
    58     l2 = p;
    59     L p3 = NULL;
    60     L p2 = NULL;
    61     if (!l1)flag = 1;
    62     if (!l2)flag = 1;
    63     while (l1)
    64     {
    65         if (!l2)break;
    66         p2 = l2;
    67         while (l2)
    68         {
    69             if (l2->id == l1->id)
    70             {
    71                 if (!l3)
    72                 {
    73                     l3 = new struct node;
    74                     l3->next = NULL;
    75                     l3->id = l2->id;
    76                     printf("%d", l3->id);
    77                     p3 = l3;
    78                 }
    79                 else
    80                 {
    81                     l3->next = new struct node;
    82                     l3->next->id = l2->id;
    83                     l3->next->next = NULL;
    84                     l3 = l3->next;
    85                     printf(" %d", l3->id);    
    86                 }
    87             }            
    88                 l2 = l2->next;
    89         }
    90         l2 = p2;
    91         l1 = l1->next;
    92     }
    93     if(flag || !p3)
    94     printf("NULL");
    95     return 0;
    96 }
  • 相关阅读:
    搭建Windows下基于Eclipse 的 PHP 开发环境
    Nuit
    typedef 用法小结
    什么是 GUID?
    软件啊
    C++ 学习经典
    [转载]OGRE初学者引导
    [转载]编程的首要原则(s)是什么?
    [转载]怎样成为优秀的软件模型设计者?
    [转载]管理角度看C++游戏程序员发展
  • 原文地址:https://www.cnblogs.com/2020R/p/12827233.html
Copyright © 2011-2022 走看看