ZZULIOJ-1153: 简易版最长序列

#include<stdio.h>
#include<string.h>
#include<math.h>
void K(int a[],int n)
{
    int num;
    for (int i = 0; i <= n - 1; i++)
    {
        for (int r = i + 1; r <= n - 1; r++)
        {
            if (a[i] > a[r])
            {
                num = a[i];
                a[i] = a[r];
                a[r] = num;
            }
        }
    }
}
int main()
{
    int n, a[10000], m, b, c;
    scanf("%d", &m);
    for (int r = 1; r <= m; r++)
    {
        memset(a, 0, sizeof(a));
        b = 1;
        c = 1;
        scanf("%d", &n);
        for (int i = 0; i <= n - 1; i++)
            scanf("%d", &a[i]);
        K(a, n);/*排序后相同项会彼此相连*/
        for (int e = 0; e <= n - 2; e++)
        {
            while (a[e] == a[e + 1])
            {
                b++;
                e++;
            }
            if (b >= c)
            {
                c = b;/*计算最长相同项长度并记录*/
            }
            b = 1;
        }
        printf("%d\n", c);
    }
    return 0;
}

上一篇:第十五章 提升用户体验 之 设计实现MVC controllers 和 actions


下一篇:zzulioj:1153: 简易版最长序列