4 条题解

  • 1
    @ 2019-03-31 13:28:12
    #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)
        {
            a[n] = x;
            n++;
        }
        void Remove(int k)
        {
            for (int i = k; i < n-1; i++)
            {
                a[i] = a[i + 1];
            }
            n--;
        }
        void SearchRemove(int x)
        {
            for (int i = 0; i < n; i++)
            {
                if (a[i] == x)
                {
                    Remove(i);
                }
            }
        }
        void print()
        {
            for (int i = 0; i < n; i++)
                cout << a[i] << " ";
        }
    };
    int main()
    {
        int n;  cin >> n;
        int b[205];
        for (int i = 0; i < n; i++)
        {
            cin >> b[i];
        }
        SeqList L;
        L.init(b, n);
        int m;  cin >> m;
        int c[205];
        for (int i = 0; i < m; i++)
        {
            cin >> c[i];
            if (c[i] > 0)
                L.Insert(c[i]);
            if (c[i] < 0)
                L.SearchRemove(-c[i]);
        }
        L.print();
        return 0;
    }
    
  • -1
    #include<iostream>
    using namespace std;
    class Array
    {
    private:
        int n;
        int size;
        int *a;
    public:
        Array(int set_n)
        {
            n=set_n;
            size=10000;
            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 reduce_minus(Array &L)//保留负数绝对值,同时去掉负数
        {//测试成功
            int flag_b=0;
            for(int i=0;i<n;i++)
            {
                if(a[i]<0&&(i!=n-1))
                {
                    L.n++;
                    L.a[flag_b]=(-a[i]);
                    flag_b++;
                    for(int j=i;j<n-1;j++)
                    {
                        a[j]=a[j+1];
                    }
                    i--;
                    n--;
                }
                if(a[i]<0&&(i==n-1))
                {
                    L.n++;
                    L.a[flag_b]=(-a[i]);
                    flag_b++;
                    n--;
                }
            }
        }
    
        void sum_up(Array &L)
        {
            int flag=0;
            for(int i=n;i<n+L.n;i++)
            {
                a[i]=L.a[flag];
                flag++;
            }
            n=n+L.n;
        }//将第一项接到第二项后面
    
        void reduce_same(Array &L)//删除和负数绝对值相等的元素,L存放的是负数绝对值
        {//测试成功
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<L.n;j++)
                {
                    if(a[i]==L.a[j]&&(i!=n-1))
                    {
                        for(int j=i;j<n-1;j++)
                        {
                            a[j]=a[j+1];
                        }
                        i--;
                        n--;
                    }
                    if(a[i]==L.a[j]&&(i==n-1))
                    {
                        n--;
                    }
                }
            }
        }
    
        void seq_list(Array &L1,Array &L2)
        {
            Array b(0);
            L2.reduce_minus(b);
            L1.sum_up(L2);
            L1.reduce_same(b);
        }   
    };
    
    int main()
    {
        Array start(0);//引导
    
        int n1,n2;
        cin>>n1;
        Array a1(n1);
        a1.set_array();
        
        /*
        Array b(0);
        a1.reduce_minus(b);
        b.output();
        */
    
        cin>>n2;
        Array a2(n2);
        a2.set_array();
        
        start.seq_list(a1,a2);
        a1.output();
    
        //a1.reduce_same(a2);
        //a1.output();
    
        //Array b1(0);
        //Array b2(0);//用来记录负数值的绝对值
        
        //a1.reduce_minus(b1);
        //a2.reduce_minus(b2);
    
        //a1.sum_up(a2);
        //a1.output();
    
        //a1.output();
        //a1.reduce_minus();去负数函数测试成功
        //a1.output();
    
        /*
        int test_n;
        cin>>test_n;
    
        Array test(test_n);
        test.set_array();
        test.output();
        */
    
        system("pause");
        return 0;
    }
    
    
  • -1
    @ 2019-04-26 19:13:58

    #include<iostream>
    #include <math.h>

    using namespace std;
    class SeqList
    {
    int n;
    // int pList[200];
    int *pList;
    int size = 200;
    public:
    SeqList();
    SeqList(int num)
    {
    n = num;
    pList = new int[size];
    }
    void setdata(int x, int i)
    {
    pList[i] = x;
    }
    int Search(int x);
    void Add(SeqList &L);
    void moveleft(int k);
    void output()
    {
    for (int i = 0; i < n; i++)
    cout << pList[i] << " ";
    cout << endl;
    }
    ~SeqList()
    {
    delete pList;
    }
    };

    SeqList::SeqList()
    {
    n = 0;
    }

    int SeqList::Search(int x)
    {
    for (int i = 0; i < n; i++)
    if (pList[i] == x)
    return i;
    return -1;
    }

    void SeqList::Add(SeqList & L)
    {
    for (int i = 0; i < L.n; i++)
    {
    if ((L.pList[i] < 0) && Search(fabs(L.pList[i])) != -1)
    moveleft(Search(fabs(L.pList[i])));
    else
    if(L.pList[i] > 0)
    pList[n] = L.pList[i], n++;
    }
    }

    void SeqList::moveleft(int k)
    {
    for (int i = k; k < n; k++)
    pList[k] = pList[k + 1];
    n--;
    }

    int main()
    {
    int num1; cin >> num1;
    SeqList L1(num1);
    int x1;
    for (int i = 0; i < num1; i++)
    cin >> x1,L1.setdata(x1, i);

    int num2; cin >> num2;
    SeqList L2(num2);
    int x2;
    for (int i = 0; i < num2; i++)
    cin >> x2, L2.setdata(x2, i);

    L1.Add(L2);
    L1.output();

    return 0;
    }

  • -3

    #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)
    {
    a[n] = x;
    n++;
    }
    void Remove(int k)
    {
    for (int i = k; i < n-1; i++)
    {
    a[i] = a[i + 1];
    }
    n--;
    }
    void SearchRemove(int x)
    {
    for (int i = 0; i < n; i++)
    {
    if (a[i] == x)
    {
    Remove(i);
    }
    }
    }
    void print()
    {
    for (int i = 0; i < n; i++)
    cout << a[i] << " ";
    }
    };
    int main()
    {
    int n; cin >> n;
    int b[205];
    for (int i = 0; i < n; i++)
    {
    cin >> b[i];
    }
    SeqList L;
    L.init(b, n);
    int m; cin >> m;
    int c[205];
    for (int i = 0; i < m; i++)
    {
    cin >> c[i];
    if (c[i] > 0)
    L.Insert(c[i]);
    if (c[i] < 0)
    L.SearchRemove(-c[i]);
    }
    L.print();
    return 0;
    }

  • 1

OO3-1 无序顺序表类的基本运算

信息

ID
1014
难度
6
分类
(无)
标签
递交数
499
已通过
148
通过率
30%
被复制
7
上传者