1038 statistics with student achievement, C

Write this question when bad idea, the idea is to the beginning of each performance are placed in a [100000], and then one by one to find, but this is too slow, resulting in a final score of points (6) points are not
the improved ideas, the result into a [101], only the statistics for each fraction appeared many times, greatly reducing the running time of this

#include <stdio.h>
int main()
{
    int n,k,i=0,x,j=0;
    int a[101] = {0};
    int c[100009] = {0};
    scanf("%d",&n);
    while(i<n)
    {
        scanf("%d",&x);
        a[x]++;
        i++;
    }   

    scanf("%d",&k);
	i=0;
    while(i<k)
    {
        scanf("%d",&x);
        c[j] = a[x];
        j++;
        i++;
    }
    j=0;
    while(j<k)
    {
        printf("%d",c[j]);
        j++;
        if(j<k)
            printf(" ");
    }
    
    return 0;
}

The following is the original idea of ​​the code, too many sets of circulation, leading to lack of time run

#include <stdio.h>
int main()
{
    int n,k,i=0,x,j=0,k1=0;
    int a[100000] = {0};
    int c[100000] = {0};
    scanf("%d",&n);
    while(i<n)
    {
        scanf("%d",&a[i]);
        i++;
    }   
    i=0;
    scanf("%d",&k);
	k1 = k;
    while(k)
    {
        scanf("%d",&x);
        i=0;
        while(i<n)
        {
            if(x==a[i])
                c[j]++;
            i++;
        }
        j++;
        k--;
    }

    j=0;
    while(j<k1)
    {
        printf("%d",c[j]);
        j++;
        if(j<k1)
            printf(" ");
    }
    
    return 0;
}
Published 44 original articles · won praise 0 · Views 872

Guess you like

Origin blog.csdn.net/weixin_43916400/article/details/104520281