1002 better one

#include <stdio.h>
#include <stdlib.h>

char s[31];

int hash()
{
        int sum = 0, i, k;

        for (i = 0, k = 0;k < 7;i++) {
                if (s[i] >='0' && s[i] <='9') {
                        sum *= 10;
                        k++;
                        sum += (s[i] - '0');
                } else if (s[i] >='A' && s[i] < 'Z') {
                        sum *= 10;
                        k++;
                        sum += ((s[i] - 'A' - (s[i] > 'Q')) / 3 + 2);
                }
        }

        return sum;
}

int mycompare(const void *a, const void *b)
{
        return (*(int*)a - *(int*)b);
}

int main(void)
{
        int len, i, num;
        int *data = NULL;
        int flag = 0;

        scanf("%d",&len);
        data = (int*)malloc(len*sizeof(int));

        for (i = 0;i < len;i++) {
                scanf("%s",s);
                data[i] = hash();
        }

        qsort(data,len,sizeof(int),mycompare);          /* It is API in stdlib */
        for (i = 0,num = 1;i < len;i+=num=1) {
                while (data[i] == data[i+1]) {
                        num++;
                        i++;
                }
                if (num > 1) {
                        printf("%03d-%04d %d\n", data[i]/10000, data[i]%10000, num);
                        flag = 1;
                }
        }
        if (flag == 0)
                printf("No duplicates.\n");

        free(data);
        return 0;
}


你可能感兴趣的:(c)