首页 > 代码库 > C++:盾神与条状项链

C++:盾神与条状项链

技术分享

技术分享

实现代码如下:

#include<cstdlib>
#include <string>
#include <iostream>
using namespace std;

int main()
{
    int n, m;
    cin >> n >> m;
    int *a = (int *)malloc(n * sizeof(n));
    for (int i = 0; i < n; i++)
        cin >> a[i];
    //定义m次操作
    for (int x = 0; x < m; x++)
    {
        int t1, t2;
        string s;
        cin >> s;
        if (s == "DEL")
        {
            cin >> t1;
            for (int i = 0; i < n; i++)
            {
                if (a[i] == t1)
                {
                    for (int j = i; j < n - 1; j++)
                        a[j] = a[j + 1];
                    break;
                }
            }
            n--;
        }
        else if (s == "ADD")
        {
            n++;
            cin >> t1 >> t2;
            for (int i = 0; i < n-1; i++)
            {
                if (a[i] == t1)
                {
                    for (int j = n - 1; j > i; j--)
                        a[j] = a[j - 1];
                    a[i] = t2;
                    break;
                }
            }
        }
    }
    cout << n << endl;
    for (int i = 0; i < n; i++)
        cout << a[i] << " ";

    system("PAUSE");
    return 0;
}

 

C++:盾神与条状项链