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;
}

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

数组方式更加简便哦,在此也给出代码

#include<iostream>
#include<cstring>
using namespace std;
#define Max 100
int a[Max];

int find(int n, int num)//查找是否存在数组中
{
    for (int i = 0; i < n; i++)
        if (a[i] == num)
            return 1;
    return 0;
}
int main()
{
    memset(a, -1, sizeof(a));
    int m, n;//容量和文章长度
    cin >> m >> n;
    int ans = 0, r = 0;
    while (n--)
    {
        int t;
        cin >> t;
        if (!find(m, t))
        {
            ans++;
            a[r] = t;
            r = (r + 1) % m;
            //使用数组的优势,不用考虑循环数组的已有个数,直接方便的后移就对了,移到循环末端自动覆盖第一个元素的值
        }
    }
    cout << ans << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/hxh88/p/9328873.html