codevs 1067 机器翻译

本题目如果学过C++ STL中的数据结构与模板库,会比较容易即可解决此题目

一种方式是,C++ deque queue都可以

另一种方式,采用循环数组来模拟队列的操作也可以

#include<iostream>
#include<string>
#include<deque>

using namespace std;
deque<int> q;
int find(int num)
{
    int len = q.size();
    for (int i = 0; i < len; i++)
    {
        if (q[i] == num)
            return 1;
    }
    return 0;
}
int main()
{
    int m, n;//容量和文章长度
    cin >> m >> n;
    int sum = 0;
    int t;
    while (n--)
    {
        cin >> t;
        if (!find(t))
        {
            sum++;
            if (q.size() >= m)
            {
                q.pop_front();
            }
            q.push_back(t);
        }
    }
    cout << sum << endl;
    return 0;
}

可以自己尝试一下,写一个数组的办法来解决此题目

猜你喜欢

转载自www.cnblogs.com/hxh88/p/9328873.html