1 条题解

  • 0
    @ 2025-2-14 21:23:51

    C :

    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    typedef struct queue {
      int data;
      struct queue *next;
    } linknode;
      linknode *front, *rear;
    void iniqueue() {
      front= (linknode *) malloc(sizeof(linknode));
      front->next = NULL;
      rear = front;
    }
    void push( int x) {
      linknode *newq;
      newq = (linknode *) malloc(sizeof(linknode));
      newq->data = x;
      newq->next = NULL;
      rear->next = newq;
      rear = newq;
    }
    void pop() {
      linknode *pops;
      if (front->next != NULL) {
        pops = front->next;
        front->next = pops->next;
        free(pops);
      } else
        rear = front;
    }
    int search(int x) {
      linknode *sear;
      sear = front->next;
      while (sear != NULL) {
        if (sear->data == x)
          return 1;
        sear = sear->next;
      }
      return 0;
    }
    int main() {
      iniqueue();
      int m, n, nn;
      scanf("%d%d", &m, &n);
      int a[n];
      nn = n;
      for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
      int count = 0, ci = 0, j = 0;
      while (nn) {
        if (search(a[j]))
        {
            
          j++;
        } else {
        	  
          if (ci >= m) {
            pop();
            push(a[j]);
            ci++;
            j++;
            count++;
          }else {  
          push (a[j] );
          ci++;
          j++;
          count++;
          }
        }
          nn--;
      }
    
      printf("%d\n", count);
      return 0;
    }
    

    C++ :

    #include <iostream>
    #include <cstring>
    #include <queue>
    
    using namespace std;
    
    #define N 1010
    
    int main()
    {
    	queue<int> q;
    	bool szExist[N] = {false};
    	int nMem = 0;
    	int nPassage = 0;
    	int nWord = 0;
    	int nCount = 0;
    	memset(szExist, false, N);
    	
    	cin >>nMem >>nPassage;
    	for (int i = 0; i < nPassage; ++i)
    	{
    		cin >>nWord;
    		if (!szExist[nWord]) // 单词没有在内存中
    		{
    			++nCount;
    			q.push(nWord);
    			szExist[nWord] = true;
    			if (q.size() > nMem)
    			{
    				szExist[q.front()] = false;
    				q.pop();
    			}
    		}
    	}
    	
    	cout <<nCount;
    	
    	return 0;
    }
    

    Java :

    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Scanner;
    public class Main {
    	public static void main(String[] args) {
    		Scanner cin = new Scanner(System.in);
    		int cap = cin.nextInt(),num = cin.nextInt();
    		int count = 0;
    		Queue<Integer> queue = new LinkedList<Integer>();
    		for(int i = 0; i < num; i ++) {
    			int temp = cin.nextInt();
    			if(!queue.contains(temp) && queue.size()<cap) {
    				queue.add(temp);
    				count ++;
    			}
    			else if(!queue.contains(temp) && queue.size() == cap) {
    				queue.poll();
    				queue.add(temp);
    				count ++;
    			}
    		}
    		System.out.println(count);
    	}
    }
    
    • 1

    信息

    ID
    895
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者