在线编程:字符串的完美度

#include <stdlib.h>

int perfect(const char *s)
{
    char    *p=s;
    int count_array[26]={0};
    int count=26;
    int idle=0;
    int i=0;
    to_upper(p);
    while(*p)
        count_array[*p++ - 'A']++;

    sort(&count_array);
    for(i=0;i<sizeof(count_array)/sizeof(int);i++)
        idle+=count_array[i]*count--;

    return idle;
}

void sort(int *array)
{
    int i=0;
    int tmp=0;
    int j=0;
    for(i=0;i<26;i++)
    {
        for(j=i;j<26;j++)
        {
            if(array[j]>array[i])
            {
                tmp=array[j];
                array[j]=array[i];
                array[i]=tmp;
            }
        }
    }
}

void to_upper(char *s)
{
    while(*s)
    {
        if(*s > 96 &&*s <= 123)
            *s=*s-32;
        *s++;
    }
}

int main()
{
    char s[20]="DAD";
    printf("pec is %d!
",perfect(s));
}



为什么没过呢?求看客指点

原文地址:https://www.cnblogs.com/dyllove98/p/3187118.html