1 solutions

  • 0
    @ 2025-11-5 16:40:38

    C++ :

    #include <iostream> 
    using namespace std;
    int main() {
    	int N, M, s2[1000], s[1000];
     	while (cin >> M >> N) {
     		int s1 = 0, j = -1, k = 0, ss = 1; 
     		for (int i = 0; i < N; i++) {  //将N个英文单词存入数组s2当中 
    			cin >> s2[i];
    		}
     		for (int c = 0; c < N; c++) {  //翻译N个英文单词 
    	 		for (int i = k; i <= j; i++) {  //检查此次翻译的单词内存中是否有 
    			 	if (s2[c] == s[i]) {
    				 	ss = 0;
     					break;
    				}
     			}
    			if (ss) {
    				s[++j] = s2[c];  //存储外存单词 
     				if(j-k+1 > M)
     					k++;  //利用k的值变化,在11行循环跳过已经被替代的单词 
     				s1++;  //外存查找次数 
    			}
     			ss = 1;
    		}
    		cout << s1 << endl;
    	}
     	return 0;   
    }
    

    Pascal :

    var
      total:longint;
      j,x,w:longint;
      m1:array[1..100] of longint;
      n1:array[1..1000] of longint;
      m,n,q:longint;
    
    function s(n:longint):boolean;
    var i:longint;
    begin
      for i:= 1 to x do
        if n=m1[i] then
          begin
            s:=true;
            exit;
          end;
      s:=false;
    end;
    
    
    begin
      fillchar(m1,sizeof(m1),0);
      fillchar(n1,sizeof(n1),0);
      read(m) ;
      readln(n);
      for w:= 1 to n-1 do
      read(n1[w]) ;
      readln(n1[n]);
    
    
      m1[1]:=n1[1];
      total:=1;
      x:=1;
      for j:=2 to n do
        if not s(n1[j]) then
          begin
            inc(total);
            if x>=m then
              begin
                for q:= 1 to x-1 do
                  m1[q]:=m1[q+1];
                m1[x]:=n1[j];
              end
            else
              begin
                inc(x);
                m1[x]:=n1[j];
              end;
          end;
      writeln(total);
    end.
    
    • 1

    Information

    ID
    17925
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By