TZC OJ 1048 简易版最长序列

题目:给你一组数(未排序),请你写设计一个程序:求出里面个数最多的数。并输出这个数的长度。
例如:给你的数是:1、 2、 3、 3、 4、 4、 5、 5、 5 、6, 其中只有6组数:1, 2, 3-3, 4-4, 5-5-5 and 6.
最长的是5那组,长度为3。所以输出3.

AC CODE:

 1 #include <stdio.h>
 2 #define MAXN 10000
 3 
 4 /* 找出最多数目数字的个数 */
 5 int Max(int array[], int n)
 6 {
 7     int max, i;
 8     for(max = array[0], i = 1; i < n; ++i)
 9     {
10         if(array[i] > max) max = array[i];
11     }
12     return max;
13 }
14 
15 /* 快速排序 */
16 int QuickSort(int r[], int s, int t)  
17 {
18     int i = s, j = t, temp;
19     if(s < t)
20     {
21         temp = r[s];
22         while(i != j)
23         {
24             while(j > i && r[j] >= temp)
25                 j--;
26             r[i] = r[j];
27             while(i < j && r[i] <= temp)
28                 i++;    
29             r[j] = r[i];
30         }    
31         r[i] = temp;
32         QuickSort(r, s, i-1);
33         QuickSort(r, i+1, t);
34     }
35 }
36 
37 int main()
38 {
39     int t, n, a[MAXN+1], b[MAXN];
40     scanf("%d",&t);
41     int i;
42     for(i = 1;i <= t; ++i)
43     {
44         scanf("%d", &n);
45         int j;
46         for(j = 0; j < n; ++j)
47         {
48             scanf("%d", &a[j]);
49             b[j] = 1;// 起始元素位置上都有一个元素,赋1
50         }
51 
52         QuickSort(a, 0, n-1);// 对a数组进行快速排序(若不是用快排,可能会出现Time OUT错误)
53 
54         int k;
55         for(k = 0, j = 1; j < n; ++j)// a[j-1]和a[j]组成了一个可移动的窗口向右移动
56         {
57             if(a[j] == a[j-1]) ++b[k];
58             else ++k;
59         }
60         printf("%d\n", Max(b, k+1));
61     }
62     return 0;
63 }

 

你可能感兴趣的:(序列)