ACM第1989題C,C++,Java各程序

問題英文描述:

Description

Farmer John's N cows (1 <= N <= 100,000) are lined up in a row.Each cow is labeled with a number in the range 1...K (1 <= K <=10,000) identifying her breed. For example, a line of 14 cows might have these breeds:

    1 5 3 2 5 1 3 4 4 2 5 1 2 3

Farmer John's acute mathematical mind notices all sorts of properties of number sequences like that above. For instance, he notices that the sequence 3 4 1 3 is a subsequence (not necessarily contiguous) of the sequence of breed IDs above. FJ is curious what is the length of the shortest possible sequence he can construct out of numbers in the range 1..K that is NOT a subsequence of the breed IDs of his cows. Help him solve this problem.

Input

* Line 1: Two integers, N and K

* Lines 2..N+1: Each line contains a single integer that is the breed ID of a cow. Line 2 describes cow 1; line 3 describes cow 2; and so on.

Output

* Line 1: The length of the shortest sequence that is not a subsequence of the input

Sample Input

14 5
1 5 3 2 5 1 3 4 4 2 5 1 2 3

Sample Output

3

 

 

問題中文描述:

描述:(是一個動態規劃問題)
農民約翰的N(1<=N<=10萬)頭奶牛排成一條隊列,每頭奶牛被貼上了一個標籤範圍在1…k(1<=k<=1萬)來識別她的品種。例如,一條線上的14頭奶牛有可能是這些品種:1 5 3 2  5 1 3 4 4 2 5 1 2 3
農民約翰敏銳的數學思維啓示各種各樣的數序就像上面的一樣。比如,他注意到他的順序3 4 1 3是上述品種的ID一個子序列(不一定是連續的)。農民約翰非常好奇的是長度儘可能短的序列,他能組成無數的號碼在範圍1…k不是一個子品種牛的ID。幫助他解決這個問題。
輸入:
第一步:輸入2個整型的數N和K(N是有N頭奶牛,K是標籤範圍)
第二步:輸入n個數,每個數是每個奶牛上的單一ID,第一個描述第一個奶牛,第二個數描述第二個奶牛,以此內推。
輸出:
1.輸出在這個序列中依次出現所有品種的最短序列的個數。
輸入舉例:
14 5
1 5 3 2 5 1 3 4 4 2 5 1 2 3
輸出舉例:
3

 

程序:

C語言的:

#include<stdio.h>
#include<memory.h>
int n,k,ans,t,i,c;
int v[10001];
int main()
{
    scanf("%d %d",&n,&k);
    ans=1;
    c=0;
    for(i=0;i<n;i++)
    {
        scanf("%d",&t);
        if(v[t]==0)
        {
            v[t]=1;
     c++;
            if(c==k)
      {
  c=0;
  ans++;
  memset(v,0,sizeof(v));
      }
        }   
    }   
    printf("%d/n",ans);
    return 0;
}

 

C++語言的:

#include <iostream>
#include <memory>
using namespace std;
int main()
{
  int n,k,i,asn=1,c=0,t;
  bool v[10001];
  memset(v,false,sizeof(v));//對布爾型數組初始化爲false
  cin>>n>>k;
  for(i=0;i<n;i++)
  {
     cin>>t;
  if(v[t]==false)
  {
     v[t]=true;
  c++;
  if(c==k)
  {
    c=0;
    asn++;
    memset(v,false,sizeof(v));//將布爾型數組全部又初始化爲false
  }
  
  }
 
  }
  cout<<asn<<endl;
  return 0;
}

 

C和C++都已經AC了、下面的Java程序正確,但是輸入不是大家所希望的。請看程序和輸出。

Java程序的:

import java.io.*;

class my1989 {
public static void main(String args[]) throws IOException {
    int asn=1,c=0,i;
    boolean[] v = new boolean[10001];
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("請輸入一個整數N:");
    String str=br.readLine( );
    int n=Integer.parseInt(str);
    System.out.println("請輸入一個整數K:");
    str=br.readLine( );
    int k=Integer.parseInt(str);
    for(i=0;i<n;++i)
    {
       BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
       System.out.println("請輸入第"+(i+1)+"個數:");
       str=br.readLine();
       int t=Integer.parseInt(str);
       if(v[t]==false)
       {
         v[t]=true;
         c++;
         if(c==k)
         {
          c=0;
          asn++;
          for(int j=1;j<=k;++j)  //for循環將數組清零
          {
           v[j]=false;
          }
         }
       } 
    } 
System.out.println(asn);
}
}

輸出結果:

請輸入一個整數N:
14
請輸入一個整數K:
5
請輸入第1個數:
1
請輸入第2個數:
5
請輸入第3個數:
3
請輸入第4個數:
2
請輸入第5個數:
5
請輸入第6個數:
1
請輸入第7個數:
3
請輸入第8個數:
4
請輸入第9個數:
4
請輸入第10個數:
2
請輸入第11個數:
5
請輸入第12個數:
1
請輸入第13個數:
2
請輸入第14個數:
3
3

Process completed.
那位大哥能把JAVA編譯通過的程序留下,小弟非常感謝。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章