3 条题解

  • 1
    @ 2021-03-13 19:51:12
    #include<iostream>
    #include<vector>
    using namespace std;
    vector<int> a;
    vector<int> b;
    int main()
    {
        int na,nb,j,j1;cin>>na;
        for(int i=0;i<na;i++)
        {
            cin>>j;
            a.push_back(j);
        }
        cin>>nb;
        for(int i=0;i<nb;i++)
        {
            cin>>j1;
            b.push_back(j1);
        }
        for(vector<int>::iterator it=b.begin();it<b.end();it++)
            a.push_back(*it);
        for(vector<int>::iterator p=a.begin();p<a.end();p++)
            for(vector<int>::iterator q=p+1;q<a.end();q++)
                if(*p>*q)
                    swap(*p,*q);
        for(int i=0;i<a.size();i++) cout<<a[i]<<" ";
        return 0;
    }
    
  • 1
    @ 2019-03-31 13:29:03
    #include <iostream>
    using namespace std;
    class SeqList
    {
        int a[205], n;
    public:
        SeqList()
        {
    
        }
        void init(int b[], int n)
        {
            this->n = n;
            for (int i = 0; i < n; i++)
            {
                a[i] = b[i];
            }
        }
        void Insert(int x)
        {
            if (x > a[n - 1])
            {
                a[n] = x;
                n++;
                return;
            }
            for (int i = 0; i < n; i++)
            {
                if (a[i] == x)
                {
                    return;
                }
                if (a[i] > x)
                {
                    n++;
                    for (int j = n; j > i; j--)
                    {
                        a[j] = a[j - 1];
                    }
                    a[i] = x;
                    break;
                }
            }
        }
        void Union(SeqList Lb)
        {
            int *b = new int[n + Lb.n];
            for (int i = 0; i < Lb.n; i++)
            {
                Insert(Lb.a[i]);
            }
            for (int i = 0; i < n; i++)
            {
                cout << a[i] << " ";
            }
        }
        void print()
        {
            for (int i = 0; i < n; i++)
                cout << a[i] << " ";
        }
    };
    int main()
    {
        int na; cin >> na;
        int b[205];
        for (int i = 0; i < na; i++)
        {
            cin >> b[i];
        }
        SeqList La;
        La.init(b, na);
    
        int nb; cin >> nb;
        int c[205];
        for (int i = 0; i < nb; i++)
        {
            cin >> c[i];
        }
        SeqList Lb;
        Lb.init(c, nb);
        La.Union(Lb);
        return 0;
    }
    
  • -2
    #include<iostream>
    using namespace std;
    
    class Array
    {
    private:
        int n;
        int size;
        int *a;
    
    public:
        Array(int set_n)
        {
            n=set_n;
            size=n*2;
            a=new int[size];
        }
    
        void set_array()
        {
            for(int i=0;i<n;i++)
            {
                cin>>a[i];
            }
        }
    
    
        void output()
        {
            for(int i=0;i<n;i++)
                cout<<a[i]<<" ";
            cout<<endl;
        }
    
        void renew()
        {
            size=size*2;
            int *newp=new int[size];
            newp=a;
            delete []a;
            a=newp;
        }
    
        void sum_up(Array &arr)//不删除相同项,只是把两个集合合在一起
        {
            n=n+arr.n;
            //if(n>=size)
                //renew();//防止第一个集合超出范围
            int flag=0;
    
            for(int i=n-arr.n;i<n;i++)
            {           
                a[i]=arr.a[flag];
                flag++;
            }
        }
    
    
        void uniqulize()
        {
            for(int i=0;i<n-1;i++)
            {
                for(int j=i+1;j<n;j++)
                {
                    if(a[i]==a[j])
                    {
                        for(int p=j;p<n-1;p++)
                        {
                            a[p]=a[p+1];
                        }
                        j--;
                        n--;
                    }
                }
            }
        }
    
        void bubble_sort()
        {
            int tmp=0;
            for(int i=0;i<n-1;i++)
            {
                for(int j=i+1;j<n;j++)
                {
                    if(a[i]>a[j])
                    {
                        tmp=a[i];
                        a[i]=a[j];
                        a[j]=tmp;
                    }
                }
            }
        }
    };
    
    int main()
    {
        int n1,n2;
        cin>>n1;
        Array a1(n1);
        a1.set_array();
    
        cin>>n2;
        Array a2(n2);
        a2.set_array();
    
        a1.sum_up(a2);
        a1.uniqulize();
        a1.bubble_sort();
        a1.output();
        
        /*
        a.output();
        a.uniqulize();
        a.output();
        */
    
        system("pause");
        return 0;
    }
    
    
  • 1

OO3-3 有序顺序表类的合并运算

信息

ID
1016
难度
3
分类
(无)
标签
递交数
244
已通过
129
通过率
53%
被复制
7
上传者