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编译通过的程序留下,小弟非常感谢。

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