#频繁元素#

#include<iostream>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        int a[n];
        for(int i=0; i<n; i++)
            cin>>a[i];
        int x[20]={0};
        for(int i=0; i<n; i++)
            x[a[i]]++;
        int m=-1, mx=-1;
        for(int i=0; i<=20; i++)
            if(x[i]>m)
            {
                m=x[i];
                mx=i;
            }
        cout<<mx<<endl;
    }
    return 0;
}

实例过了
自测输入
0
预期输出
output is null
实际输出
20

为什么输出不是0?

好像整个都不对:
如果输入的每个数都只出现0次或1次则输出20;
否则如果输入有出现次数相同则输出最小值而不是第一个出现的。


#include<iostream>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        int a[n];
        for(int i=0; i<n; i++)
            cin>>a[i];
        int x[20]={0}, b[20]={-1};
        for(int i=0; i<n; i++)
        {
            x[a[i]]++;
            for(int j=0; j<=20; j++)
            {
                if(b[j]==a[i])
                    break;
                if(b[j]==-1)
                    b[j]=a[i];
            }
        }
        int m=-1, mx=-1;
        for(int i=0; i<=20; i++)
            if(x[i]>m)
            {
                m=x[i];
                mx=i;
//                 cout<<m<<endl;
//                 cout<<mx<<endl;
            }
        cout<<mx<<endl;
    }
    return 0;
}

自测输入
1
1
预期输出
output is null
实际输出
20

#include<iostream>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        int a[n];
        for(int i=0; i<n; i++)
            cin>>a[i];
        int x[20]={0}, b[20]={-1};
        for(int i=0; i<n; i++)
        {
            x[a[i]]++;
            for(int j=0; j<=20; j++)
            {
                if(b[j]==a[i])
                    break;
                if(b[j]==-1)
                    b[j]=a[i];
            }
        }
        int m=-1, mx=-1;
        for(int i=0; i<=20; i++)
            if(x[i]>m)
            {
                m=x[i];
                mx=i;
                cout<<m<<endl;
                cout<<mx<<endl;
            }
        cout<<mx<<endl;
    }
    return 0;
}

自测输入
1
1
预期输出
output is null
实际输出
0
0
1
1
1


数组b[20]没有元素b[20],只有b[0]~b[19]!


#include<iostream>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        int a[n];
        for(int i=0; i<n; i++)
            cin>>a[i];
        int x[20]={0}, b[20]={-1};
        for(int i=0; i<n; i++)
        {
            x[a[i]]++;
            for(int j=0; j<=20; j++)
            {
                if(b[j]==a[i])
                    break;
                if(b[j]==-1)
                    b[j]=a[i];
            }
        }
        for(int i=0; i<=20; i++)
            cout<<b[i]<<endl;
        int m=-1, mx=-1;
        for(int i=0; i<=20; i++)
            if(x[b[i]]>m)
            {
                m=x[b[i]];
                mx=b[i];
//                 cout<<m<<endl;
//                 cout<<mx<<endl;
            }
//         cout<<mx<<endl;
    }
    return 0;
}

自测输入
4
1 2 2 3
预期输出
2
实际输出
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
483617600

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务