编程打卡:C++语言程序设计

//9-1
 
#include <iostream>
#include <array>
using namespace std;
int main()
{
    
    int n;
    cin >> n;
    array<double, 100000> scores;
    for (int i = 0; i < n; i++)
    {
        cin >> scores[i];
    }
    double average = 0;
    for (int i = 0; i < n; i++)
    {
        average += scores[i];
    }
    average /= n;
    cout << average << endl;
}
//9-4
class Node
{
private:
    Node<T> *next;
    Node<T> *previous;

public:
    T data;

    Node(const T &data, Node<T> *next = 0, Node<T> *previous = 0);
    void insertAfter(Node<T> *p);
    void insertBefore(Node<T> *p);
    Node<T> *deleteAfter();
    Node<T> *deleteBefore();
    Node<T> *nextNode();
    Node<T> *previousNode();
    const Node<T> *nextNode() const;
    const Node<T> *previousNode() const;
};
template <class T>
Node<T>::Node(const T &data, Node<T> *next /* = 0 */) : data(data), next(next) {}

template <class T>
Node<T> *Node<T>::nextNode()
{
    return next;
}
Node

    template <class T>
    Node<T> *Node<T>::previousNode()
{
    return previous;
}

template <class T>
const Node<T> *Node<T>::nextNode() const
{
    return next;
}

template <class T>
const Node<T> *Node<T>::previousNode() const
{
    return previous;
}

template <class T>
void Node<T>::insertAfter(Node<T> *p)
{
    p->next = next;
    next = p;
}

template <class T>
void Node<T>::insertBefore(Node<T> *p)
{
    p->previous = previous;
    previous = p;
}

template <class T>
Node<T> *Node<T>::deleteAfter()
{
    Node<T> *tempPtr = next;
    if (next == 0)
        return 0;
    next = tempPtr->next;
    return tempPtr;
}

template <class T>
Node<T> *Node<T>::deleteBefore()
{
    Node<T> *tempPtr = previous;
    if (previous == 0)
        return 0;
    previous = tempPtr->previous;
    return tempPtr;
}
//9-5
#ifndef NODE_H
#define NODE_H

template <class T>
class Node
{
private:
    Node<T> *next;

public:
    T data;

    Node(const T &data, Node<T> *next = 0);
    void insertAfter(Node<T> *p);
    Node<T> *deleteAfter();
    Node<T> *nextNode();
    const Node<T> *nextNode() const;
};

template <class T>
Node<T>::Node(const T &data, Node<T> *next /* = 0 */) : data(data), next(next) {}

template <class T>
Node<T> *Node<T>::nextNode()
{
    return next;
}

template <class T>
const Node<T> *Node<T>::nextNode() const
{
    return next;
}

template <class T>
void Node<T>::insertAfter(Node<T> *p)
{
    p->next = next;
    next = p;
}

template <class T>
Node<T> *Node<T>::deleteAfter()
{
    Node<T> *tempPtr = next;
    if (next == 0)
        return 0;
    next = tempPtr->next;
    return tempPtr;
}

#endif

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "Node.h"

#include <iostream>
using namespace std;

template <class T>
class LinkedList
{
private:
    Node<T> *front, *rear;
    Node<T> *prevPtr, *currPtr;
    int size;
    int position;

    Node<T> *newNode(const T &item, Node<T> *ptrNext = NULL);

    void freeNode(Node<T> *p);

    void copy(const LinkedList<T> &L);

public:
    LinkedList();
    LinkedList(const LinkedList<T> &L);
    ~LinkedList();
    LinkedList<T> &operator=(const LinkedList<T> &L);

    int getSize() const;
    bool isEmpty() const;

    void reset(int pos = 0);
    void next();
    bool endOfList() const;
    int currentPosition() const;

    void insertFront(const T &item);
    void insertRear(const T &item);
    void insertAt(const T &item);
    void insertAfter(const T &item);

    T deleteFront();
    void deleteCurrent();

    T &data();
    const T &data() const;

    void clear();
};

template <class T>
Node<T> *LinkedList<T>::newNode(const T &item, Node<T> *ptrNext)
{
    Node<T> *p;
    p = new Node<T>(item, ptrNext);
    if (p == NULL)
    {
        cout << "Memory allocation failure!\n";
        exit(1);
    }
    return p;
}

template <class T>
void LinkedList<T>::freeNode(Node<T> *p)
{
    delete p;
}

template <class T>
void LinkedList<T>::copy(const LinkedList<T> &L)
{
    Node<T> *p = L.front;
    int pos;
    while (p != NULL)
    {
        insertRear(p->data);
        p = p->nextNode();
    }
    if (position == -1)
        return;

    prevPtr = NULL;
    currPtr = front;
    for (pos = 0; pos != position; pos++)
    {
        prevPtr = currPtr;
        currPtr = currPtr->nextNode();
    }
}

template <class T>
LinkedList<T>::LinkedList() : front(NULL), rear(NULL),
                              prevPtr(NULL), currPtr(NULL), size(0), position(-1)
{
}

template <class T>
LinkedList<T>::LinkedList(const LinkedList<T> &L)
{
    front = rear = NULL;
    prevPtr = currPtr = NULL;
    size = 0;
    position = -1;
    copy(L);
}

template <class T>
LinkedList<T>::~LinkedList()
{
    clear();
}

template <class T>
LinkedList<T> &LinkedList<T>::operator=(const LinkedList<T> &L)
{
    if (this == &L)
        return *this;
    clear();
    copy(L);
    return *this;
}

template <class T>
int LinkedList<T>::getSize() const
{
    return size;
}

template <class T>
bool LinkedList<T>::isEmpty() const
{
    return size == 0;
}

template <class T>
void LinkedList<T>::reset(int pos)
{
    int startPos;
    if (front == NULL)
        return;
    if (pos < 0 || pos > size - 1)
    {
        cerr << "Reset: Invalid list position: " << pos << endl;
        return;
    }

    if (pos == 0)
    {
        prevPtr = NULL;
        currPtr = front;
        position = 0;
    }
    else
    {
        currPtr = front->nextNode();
        prevPtr = front;
        startPos = 1;
        for (position = startPos; position != pos; position++)
        {
            prevPtr = currPtr;
            currPtr = currPtr->nextNode();
        }
    }
}

template <class T>
void LinkedList<T>::next()
{
    if (currPtr != NULL)
    {
        prevPtr = currPtr;
        currPtr = currPtr->nextNode();
        position++;
    }
}

template <class T>
bool LinkedList<T>::endOfList() const
{
    return currPtr == NULL;
}

template <class T>
int LinkedList<T>::currentPosition() const
{
    return position;
}

template <class T>
void LinkedList<T>::insertFront(const T &item)
{
    if (front != NULL)
        reset();
    insertAt(item);
}

template <class T>
void LinkedList<T>::insertRear(const T &item)
{
    Node<T> *nNode;
    prevPtr = rear;
    nNode = newNode(item);
    if (rear == NULL)
        front = rear = nNode;
    else
    {
        rear->insertAfter(nNode);
        rear = nNode;
    }
    currPtr = rear;
    position = size;
    size++;
}

template <class T>
void LinkedList<T>::insertAt(const T &item)
{
    Node<T> *nNode;
    if (prevPtr == NULL)
    {
        nNode = newNode(item, front);
        front = nNode;
    }
    else
    {
        nNode = newNode(item);
        prevPtr->insertAfter(nNode);
    }
    if (prevPtr == rear)
    {
        rear = nNode;
        position = size;
    }
    currPtr = nNode;
    size++;
}

template <class T>
void LinkedList<T>::insertAfter(const T &item)
{
    Node<T> *p;
    p = newNode(item);
    if (front == NULL)
    {
        front = currPtr = rear = p;
        position = 0;
    }
    else
    {
        if (currPtr == NULL)
            currPtr = prevPtr;
        currPtr->insertAfter(p);
        if (currPtr == rear)
        {
            rear = p;
            position = size;
        }
        else
            position++;
        prevPtr = currPtr;
        currPtr = p;
    }
    size++;
}

template <class T>
T LinkedList<T>::deleteFront()
{
    T item;
    reset();
    if (front == NULL)
    {
        cerr << "Invalid deletion!" << endl;
        exit(1);
    }
    item = currPtr->data;
    deleteCurrent();
    return item;
}

template <class T>
void LinkedList<T>::deleteCurrent()
{
    Node<T> *p;
    if (currPtr == NULL)
    {
        cerr << "Invalid deletion!" << endl;
        exit(1);
    }
    if (prevPtr == NULL)
    {
        p = front;
        front = front->nextNode();
    }
    else
        p = prevPtr->deleteAfter();

    if (p == rear)
    {
        rear = prevPtr;
        position--;
    }
    currPtr = p->nextNode();
    freeNode(p);
    size--;
}

template <class T>
T &LinkedList<T>::data()
{
    if (size == 0 || currPtr == NULL)
    {
        cerr << "Data: invalid reference!" << endl;
        exit(1);
    }
    return currPtr->data;
}

template <class T>
void LinkedList<T>::clear()
{
    Node<T> *currPosition, *nextPosition;
    currPosition = front;
    while (currPosition != NULL)
    {
        nextPosition = currPosition->nextNode();
        freeNode(currPosition);
        currPosition = nextPosition;
    }
    front = rear = NULL;
    prevPtr = currPtr = NULL;
    size = 0;
    position = -1;
}

#endif

#include <iostream>
#include <cstdlib>
#include "LinkedList.h"
using namespace std;

int main()
{

    LinkedList<int> a, b;

    for (int i = 0; i < 5; i++)
    {
        a.insertRear(i);
        b.insertRear(i);
    }
    a.insertRear(b);
    a.reset();
    while (!a.endOfList())
    {
        cout << a.data() << " ";
        a.next();
    }
}
//9-6
#ifndef NODE_H
#define NODE_H

template <class T>
class Node
{
private:
    Node<T> *next;

public:
    T data;

    Node(const T &data, Node<T> *next = 0);
    void insertAfter(Node<T> *p);
    Node<T> *deleteAfter();
    Node<T> *nextNode();
    const Node<T> *nextNode() const;
};

template <class T>
Node<T>::Node(const T &data, Node<T> *next /* = 0 */) : data(data), next(next) {}

template <class T>
Node<T> *Node<T>::nextNode()
{
    return next;
}

template <class T>
const Node<T> *Node<T>::nextNode() const
{
    return next;
}

template <class T>
void Node<T>::insertAfter(Node<T> *p)
{
    p->next = next;
    next = p;
}

template <class T>
Node<T> *Node<T>::deleteAfter()
{
    Node<T> *tempPtr = next;
    if (next == 0)
        return 0;
    next = tempPtr->next;
    return tempPtr;
}

#endif

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "Node.h"

#include <iostream>
using namespace std;

template <class T>
class LinkedList
{
private:
    Node<T> *front, *rear;
    Node<T> *prevPtr, *currPtr;
    int size;
    int position;

    Node<T> *newNode(const T &item, Node<T> *ptrNext = NULL);

    void freeNode(Node<T> *p);

    void copy(const LinkedList<T> &L);

public:
    LinkedList();
    LinkedList(const LinkedList<T> &L);
    ~LinkedList();
    LinkedList<T> &operator=(const LinkedList<T> &L);

    int getSize() const;
    bool isEmpty() const;

    void reset(int pos = 0);
    void next();
    bool endOfList() const;
    int currentPosition() const;

    void insertFront(const T &item);
    void insertRear(const T &item);
    void insertAt(const T &item);
    void insertAfter(const T &item);

    T deleteFront();
    void deleteCurrent();

    T &data();
    const T &data() const;

    void clear();
};

template <class T>
Node<T> *LinkedList<T>::newNode(const T &item, Node<T> *ptrNext)
{
    Node<T> *p;
    p = new Node<T>(item, ptrNext);
    if (p == NULL)
    {
        cout << "Memory allocation failure!\n";
        exit(1);
    }
    return p;
}

template <class T>
void LinkedList<T>::freeNode(Node<T> *p)
{
    delete p;
}

template <class T>
void LinkedList<T>::copy(const LinkedList<T> &L)
{
    Node<T> *p = L.front;
    int pos;
    while (p != NULL)
    {
        insertRear(p->data);
        p = p->nextNode();
    }
    if (position == -1)
        return;

    prevPtr = NULL;
    currPtr = front;
    for (pos = 0; pos != position; pos++)
    {
        prevPtr = currPtr;
        currPtr = currPtr->nextNode();
    }
}

template <class T>
LinkedList<T>::LinkedList() : front(NULL), rear(NULL),
                              prevPtr(NULL), currPtr(NULL), size(0), position(-1)
{
}

template <class T>
LinkedList<T>::LinkedList(const LinkedList<T> &L)
{
    front = rear = NULL;
    prevPtr = currPtr = NULL;
    size = 0;
    position = -1;
    copy(L);
}

template <class T>
LinkedList<T>::~LinkedList()
{
    clear();
}

template <class T>
LinkedList<T> &LinkedList<T>::operator=(const LinkedList<T> &L)
{
    if (this == &L)
        return *this;
    clear();
    copy(L);
    return *this;
}

template <class T>
int LinkedList<T>::getSize() const
{
    return size;
}

template <class T>
bool LinkedList<T>::isEmpty() const
{
    return size == 0;
}

template <class T>
void LinkedList<T>::reset(int pos)
{
    int startPos;
    if (front == NULL)
        return;
    if (pos < 0 || pos > size - 1)
    {
        cerr << "Reset: Invalid list position: " << pos << endl;
        return;
    }

    if (pos == 0)
    {
        prevPtr = NULL;
        currPtr = front;
        position = 0;
    }
    else
    {
        currPtr = front->nextNode();
        prevPtr = front;
        startPos = 1;
        for (position = startPos; position != pos; position++)
        {
            prevPtr = currPtr;
            currPtr = currPtr->nextNode();
        }
    }
}

template <class T>
void LinkedList<T>::next()
{
    if (currPtr != NULL)
    {
        prevPtr = currPtr;
        currPtr = currPtr->nextNode();
        position++;
    }
}

template <class T>
bool LinkedList<T>::endOfList() const
{
    return currPtr == NULL;
}

template <class T>
int LinkedList<T>::currentPosition() const
{
    return position;
}

template <class T>
void LinkedList<T>::insertFront(const T &item)
{
    if (front != NULL)
        reset();
    insertAt(item);
}

template <class T>
void LinkedList<T>::insertRear(const T &item)
{
    Node<T> *nNode;
    prevPtr = rear;
    nNode = newNode(item);
    if (rear == NULL)
        front = rear = nNode;
    else
    {
        rear->insertAfter(nNode);
        rear = nNode;
    }
    currPtr = rear;
    position = size;
    size++;
}

template <class T>
void LinkedList<T>::insertAt(const T &item)
{
    Node<T> *nNode;
    if (prevPtr == NULL)
    {
        nNode = newNode(item, front);
        front = nNode;
    }
    else
    {
        nNode = newNode(item);
        prevPtr->insertAfter(nNode);
    }
    if (prevPtr == rear)
    {
        rear = nNode;
        position = size;
    }
    currPtr = nNode;
    size++;
}

template <class T>
void LinkedList<T>::insertAfter(const T &item)
{
    Node<T> *p;
    p = newNode(item);
    if (front == NULL)
    {
        front = currPtr = rear = p;
        position = 0;
    }
    else
    {
        if (currPtr == NULL)
            currPtr = prevPtr;
        currPtr->insertAfter(p);
        if (currPtr == rear)
        {
            rear = p;
            position = size;
        }
        else
            position++;
        prevPtr = currPtr;
        currPtr = p;
    }
    size++;
}

template <class T>
T LinkedList<T>::deleteFront()
{
    T item;
    reset();
    if (front == NULL)
    {
        cerr << "Invalid deletion!" << endl;
        exit(1);
    }
    item = currPtr->data;
    deleteCurrent();
    return item;
}

template <class T>
void LinkedList<T>::deleteCurrent()
{
    Node<T> *p;
    if (currPtr == NULL)
    {
        cerr << "Invalid deletion!" << endl;
        exit(1);
    }
    if (prevPtr == NULL)
    {
        p = front;
        front = front->nextNode();
    }
    else
        p = prevPtr->deleteAfter();

    if (p == rear)
    {
        rear = prevPtr;
        position--;
    }
    currPtr = p->nextNode();
    freeNode(p);
    size--;
}

template <class T>
T &LinkedList<T>::data()
{
    if (size == 0 || currPtr == NULL)
    {
        cerr << "Data: invalid reference!" << endl;
        exit(1);
    }
    return currPtr->data;
}

template <class T>
void LinkedList<T>::clear()
{
    Node<T> *currPosition, *nextPosition;
    currPosition = front;
    while (currPosition != NULL)
    {
        nextPosition = currPosition->nextNode();
        freeNode(currPosition);
        currPosition = nextPosition;
    }
    front = rear = NULL;
    prevPtr = currPtr = NULL;
    size = 0;
    position = -1;
}

#endif

template <class T>
class OrderedList : public LinkedList<T>
{
public:
    OrderedList();
    OrderedList(const OrderedList<T> &L);
    ~OrderedList();
    OrderedList<T> &operator=(const OrderedList<T> &L);
    void insert(const T &item);
    bool search(const T &item);
    void deleteItem(const T &item);
};

template <class T>
OrderedList<T>::OrderedList() : LinkedList<T>() {}

template <class T>
OrderedList<T>::OrderedList(const OrderedList<T> &L) : LinkedList<T>(L) {}

template <class T>
OrderedList<T>::~OrderedList() {}

template <class T>
OrderedList<T> &OrderedList<T>::operator=(const OrderedList<T> &L)
{
    LinkedList<T>::operator=(L);
    return *this;
}

template <class T>
void OrderedList<T>::insert(const T &item)
{
    LinkedList<T>::reset();
    while (!LinkedList<T>::endOfList() && LinkedList<T>::currPtr->data < item)
        LinkedList<T>::next();
    LinkedList<T>::insertAt(item);
}

template <class T>
bool OrderedList<T>::search(const T &item)
{
    LinkedList<T>::reset();
    while (!LinkedList<T>::endOfList() && LinkedList<T>::currPtr->data < item)
        LinkedList<T>::next();
    return !LinkedList<T>::endOfList() && LinkedList<T>::currPtr->data == item;
}

template <class T>
void OrderedList<T>::deleteItem(const T &item)
{
    LinkedList<T>::reset();
    while (!LinkedList<T>::endOfList() && LinkedList<T>::currPtr->data < item)
        LinkedList<T>::next();
    if (!LinkedList<T>::endOfList() && LinkedList<T>::currPtr->data == item)
        LinkedList<T>::deleteCurrent();
}

#include <iostream>
#include <cstdlib>
#include "LinkedList.h"
using namespace std;

int main()
{
    OrderedList<int> c, d;

    for (int i = 0; i < 5; i++)
    {
        c.insert(i);
        d.insert(i);
    }
    c.insert(d);
    c.reset();
    while (!c.endOfList())
    {
        cout << c.data() << " ";
        c.next();
    }

    cout << endl;
}
//9-10
#include <iostream>
using namespace std;

template <class T>
void insertionSort(T a[], int n)
{
    int i, j;
    T temp;

    for (int i = 1; i < n; i++)
    {

        int j = i;
        T temp = a[i];
        while (j > 0 && temp < a[j - 1])
        {

            a[j] = a[j - 1];
            j--;
        }

        a[j] = temp;
    }
}

int main()
{
    int data1[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
    insertionSort(data1, 20);
    for (int i = 0; i < 20; i++)
    {
        cout << data1[i] << " ";
    }
}
//9-12
#include <iostream>
using namespace std;
template <class T>
void mySwap(T &x, T &y)
{
    T temp = x;
    x = y;
    y = temp;
}

template <class T>
void selectionSort(T a[], int n)
{
    for (int i = 0; i < n - 1; i++)
    {
        int leastIndex = i;
        for (int j = i + 1; j < n; j++)
            if (a[j] < a[leastIndex])
                leastIndex = j;
        mySwap(a[i], a[leastIndex]);
    }
}

int main()
{
    int data1[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
    selectionSort(data1, 20);
    for (int i = 0; i < 20; i++)
    {
        cout << data1[i] << " ";
    }
}
//9-14
#include <iostream>
using namespace std;
template <class T>
void mySwap(T &x, T &y)
{
    T temp = x;
    x = y;
    y = temp;
}

template <class T>
void bubbleSort(T a[], int n)
{
    int i = n - 1;
    while (i > 0)
    {
        int lastExchangeIndex = 0;
        for (int j = 0; j < i; j++)
            if (a[j + 1] < a[j])
            {
                mySwap(a[j], a[j + 1]);
                lastExchangeIndex = j;
            }
        i = lastExchangeIndex;
    }
}

int main()
{
    int data1[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
    bubbleSort(data1, 20);
    for (int i = 0; i < 20; i++)
    {
        cout << data1[i] << " ";
    }
}
//9-15
#include <iostream>
using namespace std;
template <class T>
void mySwap(T &x, T &y)
{
    T temp = x;
    x = y;
    y = temp;
}

template <class T>
void bubbleSort(T a[], int n)
{
    int i = n - 1;
    while (i > 0)
    {
        int lastExchangeIndex = 0;
        for (int j = 0; j < i; j++)
            if (a[j + 1] > a[j])
            {
                mySwap(a[j], a[j + 1]);
                lastExchangeIndex = j;
            }
        i = lastExchangeIndex;
    }
}

int main()
{
    int data1[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
    bubbleSort(data1, 20);
    for (int i = 0; i < 20; i++)
    {
        cout << data1[i] << " ";
    }
}
//9-17
#include <iostream>
using namespace std;

template <class T>
int seqSearch(const T list[], int n, const T &key) {
	for(int i = 0; i < n; i++)
		if (list[i] == key)
			return i;
	return -1;
}

int main()
{
    int a;
    int data1[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
    cout << "Enter a number: ";
    cin >> a;
    int index = seqSearch(data1, 20, a);
    cout << "The index of " << a << " is " << index << endl;
}
//9-19
#include <iostream>
using namespace std;

template <class T>
void bubbleSort(T a[], int n)
{
    int i = n - 1;
    while (i > 0)
    {
        int lastExchangeIndex = 0;
        for (int j = 0; j < i; j++)
            if (a[j + 1] < a[j])
            {
                mySwap(a[j], a[j + 1]);
                lastExchangeIndex = j;
            }
        i = lastExchangeIndex;
    }
}

template <class T>
int binSearch(const T list[], int n, const T &key)
{
    int low = 0;
    int high = n - 1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (key == list[mid])
            return mid;
        else if (key < list[mid])
            high = mid - 1;
        else
            low = mid + 1;
    }
    return -1;
}

int main()
{
    int a;
    int data1[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
    cout << "Enter a number: ";
    cin >> a;
    bubbleSort(data1, 20);
    int index = binSearch(data1, 20, a);
    cout << "The index of " << a << " is " << index << endl;
}
//9-24
#include <iostream>
using namespace std;

template <unsigned M, unsigned N> struct Permutation
{
    enum {VALUE = Permutation<M-1,N>::VALUE * (N-M+1)};
};
template <unsigned N> struct Permutation<0,N>
{
    enum {VALUE = 1};
};

int main()
{
    cout << Permutation<3,5>::VALUE << endl;
    return 0;
}
//10-3
 
#include <iostream>
#include <vector>
using namespace std;
vector<int> v;
int main()
{
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
        cout << v.capacity() << endl;
    }
}
//10-7
 
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
int main()
{
    stack<int> s;
    queue<int> q;
    priority_queue<int> pq;
    s.push(5);
    s.push(1);
    s.push(4);
    s.push(6);
    s.pop();
    s.pop();
    s.push(2);
    s.push(3);
    s.pop();
    s.pop();
    while (!s.empty())
    {
        cout << s.top() << " ";
        s.pop();
    }
    cout << endl;
    q.push(5);
    q.push(1);
    q.push(4);
    q.push(6);
    q.pop();
    q.pop();
    q.push(2);
    q.push(3);
    q.pop();
    q.pop();
    while (!q.empty())
    {
        cout << q.front() << " ";
        q.pop();
    }
    cout << endl;
    pq.push(5);
    pq.push(1);
    pq.push(4);
    pq.push(6);
    pq.pop();
    pq.pop();
    pq.push(2);
    pq.push(3);
    pq.pop();
    pq.pop();
    while (!pq.empty())
    {
        cout << pq.top() << " ";
        pq.pop();
    }
    cout << endl;
    return 0;
}
//10-8
 
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
int main()
{
    map<string, int> m;
    set<string> s;
    string word;
    while (cin >> word)
    {
        if (s.find(word) == s.end())
        {
            s.insert(word);
            m[word] = 1;
        }
        else
        {
            m[word]++;
        }
    }
    for (map<string, int>::iterator it = m.begin(); it != m.end(); it++)
    {
        cout << it->first << " " << it->second << endl;
    }
    return 0;
}
//10-10
 
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    vector<int> v1, v2, v3, v4;
    int n, m, t;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> t;
        v1.push_back(t);
    }
    cin >> m;
    for (int i = 0; i < m; i++)
    {
        cin >> t;
        v2.push_back(t);
    }
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(v3));
    set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(v4));
    cout << "交集:";
    for (int i = 0; i < v3.size(); i++)
    {
        cout << v3[i] << " ";
    }
    cout << endl;
    cout << "并集:";
    for (int i = 0; i < v4.size(); i++)
    {
        cout << v4[i] << " ";
    }
    cout << endl;
    cout << "差集:";
    set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(v3));
    for (int i = 0; i < v3.size(); i++)
    {
        cout << v3[i] << " ";
    }
    cout << endl;
    return 0;
}
 
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
    vector<int> v(10);
    srand(time(NULL));
    generate(v.begin(), v.end(), rand);
    for (auto i : v)
        cout << i << " ";
    cout << endl;
    return 0;
}

posted @ 2023-05-11 20:57  satou_matsuzaka  阅读(9)  评论(0编辑  收藏  举报

This is a Test

メイドノココロハ アヤツリドール